Jump to content

JavaScript Div/Non JavaScript Page


Dysan

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.