So I have a link: <a href="index.php" onclick="js_function()">Link Text</a> Code (markup): What I want to do is when a user clicks the link, the JS function is run, but the browser does not navigate to index.php. However, if a user has Javascript disabled and clicks on the link, I'd like the browser to go to index.php. Is there any way - in Javascript or PHP - to do this. Thanks, BP
1) Change the link code to: <a href="index.php" onclick="return js_function();">Link Text</a> Code (markup): 2) Add a return false; statement before the Javascript function terminates: function js_function () { // Do something... return false; } Code (markup):
I Think that he wants that the href will work. blueparukia if you want the Href to work, do return true, if you don't want it to work do return false . Good luck
Yes I have tried it, and return false does what I want - the href won't work. However if I disable Javscript, it works. Thanks all.
Would this work when calling a function on a parent frame? My code is still following the link. //topframe.php echo "<a href=index.php onClick=\"return parent.myPopup($viewedid);\">Click here!</a>"; //frameset.php <html><head><script> function myPopup(id) { window.open("checkusername.php$id=" + id, "popUp", "status = 1, height = 100, width = 200, resizable = 0" ) popUp.focus(); return false; } </script></head> Code (markup): The popup window shows up fine, but the topframe still follows the link to index.html. I only put up the function related code, there's a good one or two hundred lines on each page that shouldn't really matter. The frameset code is working fine though.
With mootools ... <a id="foo" [...] HTML: <script type="text/ecmascript"><![CDATA[/* $('foo').addEvent('click', function(e) { new Event(e).stop(); parent.myPopup(bar); return; }); */]]></script> Code (markup): Since you're passing an ID to myPopup, I'd recommend giving your anchor tag an id of "foo_$viewid". Then, in the click event handler above, instead of bar, use: parent.myPopup(this.id.match(/_(\d)+$/)[1]); Code (markup): I use that style all the time. It's very slick.