Mobile app version of vmapp.org
Login or Join
Bethany197

: How to have html display a link, but open a javascipt function instead? I have an html page, and I need a link to show that the user would be going to 'example.html', when really, the link

@Bethany197

Posted in: #Html #Javascript #Links

I have an html page, and I need a link to show that the user would be going to 'example.html', when really, the link goes to 'javascipt:ajaxLoad(example.html);'.

EDIT: I went over to stackexchange, and their solution worked. The html would go from this:
Example
to this:
Example

The solution, is if javascript is enabled, the 'return false;' will cancel going to the link on the page, and load the javascript. If not, then it will go to the page in the href

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Bethany197

1 Comments

Sorted by latest first Latest Oldest Best

 

@Kevin317

Have the HTML with a working link in it that leads to the content you want to show. Then use JavaScript to stop the browser from taking them to that page and then use Ajax to show the content to them in their current page.

This is known as progressive enhancement and allows everyone to see the content whether they have JavaScript enabled or not. How to write this code will vary depending on what JavaScript framework you use (Prototype, jQuery, MooTools, etc). But the psuedocode would look something like this:


on DOM load attach an event monitor to that link
when that link is clicked stop the default event (the browser loads the new page)
Make your ajax call to get the new content
Replace the existing content with the new content


If you want code help you will need to go to StackOverflow as that is where coding questions are handled. This question in its current form would probably be closed there since it doesn't show any effort to write some code. So I suggest starting to write your own code and then if you have trouble ask for help there showing whatever code you have so far.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme