I figured out that it definitely had to do with the fact my link was in a popup window. I also had another in an IFrame. Both would not work. I basically had to keep the function definition in the main page that would either popup the window or contain the IFrame. Then when a user clicks on a link inside a document displayed using either the IFrame or new popup window, the clicked link would have to refer back to the original main window and execute that function.
Here is the code that goes in the parent document that produces the popup window:
In the head of the parent document, I have the following blank script import:
Code:
<script id='alertTrackerScript' type='text/javascript'></script>
// Used to track alerts. This is called by the actual alert document.
function trackAlert()
{
if(document.getElementById && document.all)
{
srcLink = "url to my script on the server";
document.getElementById('alertTrackerScript').src = srcLink;
}
return true;
}
Then in the popup window document I have the following JavaScript function:
Code:
function trackAlert()
{
// weeds out non IE browsers b/c it only works in IE
if(document.getElementById && document.all)
{
window.opener.trackAlert();
}
return true;
}
function openRegUrl(url)
{
window.opener.location = url;
window.close();
}
Then my link in the popup window is . . .
Code:
<a href="javascript<IMG title=embarrasment alt="" src="images/smilies/redface.gif" border=0>penRegUrl('http://www.cnn.com')" onClick="trackAlert()">cnn</a>
The trick here is that when the user clicks on the link, the trackAlert() function is called which calls the trackAlert() function in the parent document. The parent document's trackAlert() then calls the script on the server. After this is finished, the function returns a true which then causes the browser to open the actual requested url.
Caveats:
1) Only works in IE. For me this is an extranet and I know for a fact that 97% of my users are IE 5.x and above.
2) It looks as if the script call is only called once when initially executed. If the user refreshes the parent page and then calls the script, then it will work again. I believe this is because the browser caches the script import's url after it is assigned. Refreshing the page resets it to unassigned allowing you to reassign it.
Bookmarks