SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up JavaScript, AJax's can I do this?

    I know some about javascript, but not much with ajax.

    This is what I need done. I have a link wraped with php code, I want to use a javascript onclick event with it but I don't want to make a new link on the page nor do I want to integrate the onlick event by wrapping the link with javascript. It would be to much work for me to take a part and reprogram the php link on the page since it's saturated with other server side code.

    What I want to know and do is some how code a hidden program on the same page that knows when the php link has been clicked and if it has been clicked the code would pop up a second tab url to another page when the browser is loading the php link.

    Basically a pop up. I don't know how to do this without using

    Code:
    <a href="#" onClick="window.open('http://link1.com'); window.open('http://link2.com');">link text</a>
    Since the php link url will not be placed in the href="#". Can ajax be able to know when a separate link has been clicked and onload create a pop up to a separate page?

    Thanks a million boys/girls

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Carnivalcity View Post
    Code:
    <a href="#" onClick="window.open('http://link1.com'); window.open('http://link2.com');">link text</a>
    Since the php link url will not be placed in the href="#". Can ajax be able to know when a separate link has been clicked and onload create a pop up to a separate page?
    Well you can forget about ajax, as that's only used to send and receive information to the server without having to reload the page.

    What you want here instead is a standard technique for scripting to assign an onclick event to a DOM element, that in this case being the anchor node.

    The easiest way to do this for one link is to supply the link with a unique identifier, so that you can then make good use of that from the script.

    You will want to place the script at the end of the body, just before the </body> tag, so that you can most easily work with the page elements.

    Code html4strict:
    <html>
    <body>
        <a id="putLinkNameHere" href="#">link text</a>
        ...
        <script src="script.js"></script>
    </body>

    The script uses the identifier to gain a reference to the link, so that it can then set the onclick event for that link.

    Code javascript:
    function linkClickHandler () {
        location.href = 'http://link1.com'; // open in main window
        window.open('http://link2.com'); // open in new window/tab
    }
     
    var link = document.getElementById('putLinkNameHere');
    link.onclick = linkClickHandler;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Carnivalcity View Post
    Code:
    <a href="#" onClick="window.open('http://link1.com'); window.open('http://link2.com');">link text</a>
    Since the php link url will not be placed in the href="#". Can ajax be able to know when a separate link has been clicked and onload create a pop up to a separate page?
    Well you can forget about ajax, as that's only used to send and receive information to the server without having to reload the page.

    What you want here instead is a standard technique for scripting to assign an onclick event to a DOM element, that in this case being the anchor node.

    The easiest way to do this for one link is to supply the link with a unique identifier, so that you can then make good use of that from the script.

    You will want to place the script at the end of the body, just before the </body> tag, so that you can most easily work with the page elements.

    Code html4strict:
    <html>
    <body>
        <a id="putLinkNameHere" href="#">link text</a>
        ...
        <script src="script.js"></script>
    </body>

    The script uses the identifier to gain a reference to the link, so that it can then set the onclick event for that link.

    Code javascript:
    function linkClickHandler () {
        location.href = 'http://link1.com'; // open in main window
        window.open('http://link2.com'); // open in new window/tab
    }
     
    var link = document.getElementById('putLinkNameHere');
    link.onclick = linkClickHandler;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •