SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: JS question

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS question

    This is just a quick question, so I can focus on planning how to write code. In a link does the onClick override the url the link takes you to? So if they have javascript enabled it will execute a function, but if not, it'll proceed to a new page.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, it does that in addition to the default action of clicking a link, which is to take you to the URL pointed to by the href attribute. If you want the onclick event handler to stop the link from being followed, you have to prevent the default action:
    Code:
    something.onclick = function(e) {
      if (window.event) window.event.returnValue = false; // IE
      else e.preventDefault();  // Decent browsers
      // other stuff
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, just a mention of what I'm doing. When a page number is clicked I send the page number to a php script and the content is sent back. The link would be like:
    Code:
    <a href="mysite.com/site-news/test-post/2" onClick="showPage(2);">Page 2</a>
    So would I put your function inside of mine? The JS would be processed fast enough to stop the page from forwarding, although I only want it to for non-js users.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you're using inline event handlers, which is when you put onclick="..." or onmouseover="..." in your HTML, then the approach is different.

    Code:
    <a href="mysite.com/site-news/test-post/2" onClick="showPage(2); return false;">Page 2</a>
    Note that inline event handlers belong in the last century.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what would you recommend? What I'm doing is there is paginated links at the bottom of the post, instead of loading the whole page again, I'm just loading in the next content of the article with AJAX. Only way I could think of doing that was with onClick.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use the code I posted in my first post, where something is a reference to the link in question, using document.getElementById or getElementsByTagName, or whatever DOM method is more appropriate.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •