javascript - <a> onClick event but still proceed to href target -
i have link follows:
<a onclick="runsomejsonpcall();" href="www.goherenext.com"> make work </a>
the javascript method basic jsonp call, this:
function runsomejsonpcall() { var script = document.createelement("script"); script.src = "http://jsonpurl.com?parameter1=" + "someparam"; document.body.appendchild(script); }
my jsonp call never executes, unless put in return false onlick event, of course href doesn't navigate target.
how both things work?
note: can use pure javascript only, no libraries
you should prevent default event ... there : event.preventdefault ...
function runsomejsonpcall(event) { if (!event) var event = window.event; if (event.preventdefault) { event.preventdefault(); }else{ // ie event.returnvalue = false; } var script = document.createelement("script"); script.src = "http://jsonpurl.com?parameter1=" + "someparam"; document.body.appendchild(script); // should wait jsonp loading // here use id retreive element can way ... settimeout("changelocation('"+ document.getelementbyid('linkid').href +"')", 1000); } changelocation(location) { window.location.href = location; }
personnaly, don't use of inline onclick attribute, i'll use addeventlistener or attachevent ... think it's beeter way :)
Comments
Post a Comment