Jump to content

Recommended Posts

Hi,

 

If JavaScript is turned ON!:

 

Using JavaScript, how do I display a div upon a link being clicked?

 

If JavaScript is OFF!

 

How do I navigate to a different page upon the link being clicked?

 

Go here for example: http://codenewbie.com/forum/ - Click the "Search" button near top-right of page! :)

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/101106-javascript-divnon-javascript-page/
Share on other sites

First, create a link and set the href to be the page you want to navigate to if javascript is turned off. Also give it an ID.

 

<a href="no_javascript.page" id="target_link">link</a>

 

Then you target that link with javascript. I put this in an external file:

 

function setLinkListener()
{
   var target = document.getElementbyId("target_link")
   target.onclick = function()
   {
      // set  the details of what you want to happen when the link is clicked
      // and javascript is on
      return false
   }
}

window.onload = function()
{
   setLinkListener()
}

 

What this does is first defines a function that finds the link in question using the ID that the link was given. After it has found that link, it listens for it to be clicked. If it is clicked, then the code is executed, and returns false. By returning false, the page will not advance to the href value in the <a > tag, and only the javascript will be executed.

 

Finally I set that function to be run once the page is loaded using window.onload.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.