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

<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 :smiley:

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.


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


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;

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.


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


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;