I have <a>s with onclick events:
<a href="#" onclick="window.open('TrackPackage.asp', '', 'location=1,menubar=1,scrollbars=1,status=1,resizable=1,width=635,height=460'); return false;" class="nounderline">Track Your Package »</a>
How can I prepend those onclick events with http://www.example.com so that the result will be:
<a href="#" onclick="window.open('http://www.example.com/TrackPackage.asp', '', 'location=1,menubar=1,scrollbars=1,status=1,resizable=1,width=635,height=460'); return false;" class="nounderline">Track Your Package »</a>
It has to be compatible with jQuery 1.4.2
Somebody else, had mentioned something like this, but I can’t get it to work in 1.4.2:
var link = $("a"); // I don't have enough info to tell you how to precisely get this instance
var originalOnClick = link.attr("onclick");
var part1 = "window.open('"; // this is always the same, right?
var part2 = originalOnClick.substr(part1.length); // the remainder, beginning with TrackPackage.asp
var newOnClick = part1 + "http://www.example.com/" + part2;
link.attr("onclick", newOnClick);
Thanks.
I don’t think
window.open()is a good idea here. It will get blocked by most popup blockers and if you’re using jQuery you shouldn’t be using the inlineonclickevent anyway.What you’re trying to do can probably be achieved with a simple anchor link:
And then you can do something like this:
EDIT:
If you don’t have control over the html and you need to do it like that then use
replace()on theonClickattribute like:example: http://jsfiddle.net/elclanrs/AH4As/