I am working on a website where I do not have direct access to all the source files, but can add extra functions using Javascript. What I am trying to do is create a button which adds "?action=delete" to the end of whatever the page's URL is, considering that this button (well, a link formatted as a button) will load on every page on the site (hence why I can't just put the URL straight into the function). I have already got to the stage where I can produce the button and make it look as I wish it to, and in the right position, but am struggling to get the href parameter to work with any page identifying Javascript I have already found. Here is what I have so far:
$('a.wikia-button.talk').after('<div><a class="wikia-button" title="Go to normal delete page" href="???">Delete</a></div>');
The three question marks are where I don't know what to put. One obvious problem I have found in trying to get this to work is to do with quotes: single and double quotes have both already been used, and so can't be used again within the href parameter (at least, not in any way I know of). This obviously makes it hard to identify a string to add the "?action=delete" to the end of the URL, and I'm guessing is the main problem causing my function to break in all the ways I've previously tried.
Any help you could give would be most appreciated! I would also like to point out that I'm not that good with Javascript, and usually just stick to altering others' functions. However, I haven't so far found any other person with this specific Javascript nestling problem to steal their code, so I hope you can help out!
~Imamadmad
First, quotes are no problem. I am a regular user at Wikia, and I know for a fact that you don't need quotes around any parameters in the url.
Second, you have two choices here: work out the correct href parameter, or use an onclick attribute.
Choice one: we first work out the href needed:
var where = window.location.href + "?action=delete";
and then we add the button, like so:
$('a.wikia-button.talk').after('<div><a class="wikia-button" title="Go to normal delete page" href=' + where + '>Delete</a></div>');
Choice two: we delegate an onclick event to the button:
$("div").on('click', "#deleteButton", function() {
var where = window.location.href + "?action=delete";
window.location.href = where;
});
and then we add the button, like so (note the absence of an href
attribute):
$('a.wikia-button.talk').after('<div><a class="wikia-button" title="Go to normal delete page" id="deleteButton">Delete</a></div>');