...

View Full Version : how to optimize multiple popup link code?



llizard
04-27-2004, 07:40 PM
I have a page with several links to open in the same popup window. This is the coding I am using:



<a href="URL_1" title="link name 1"
onclick="var w=window.open('URL_1', 'popup', 'scrollbars=1,resizable=1,width=590,height=400,left=20,top=20'); return false;"
onmouseover="setAttribute('title',' &quot;link 1&quot; opens in a popup')">link 1</a>

<a href="URL_2" title="link name 2"
onclick="var w=window.open('URL_2', 'popup', 'scrollbars=1,resizable=1,width=590,height=400,left=20,top=20'); return false;"
onmouseover="setAttribute('title',' &quot;link 2&quot; opens in a popup')">link 2</a>

<a href="URL_3" etc.etc.


I am a rank beginner at javascripting and wonder how to go about putting most of the popup coding into the header so that I am not repeating the same rather large amount of code over and over in each link.



<script type="text/javascript">
<!--
???
// -->
</script>
</head>
<body>
<a href="URL_1" title="link name 1"
onclick="var w=window.open('URL_1', 'a var???'); return false;"
onmouseover="setAttribute('title','&quot;link 1&quot; a var???')">link 1</a>

<a href="URL_2" title="link name 2"
onclick="var w=window.open('URL_2', a var???); return false;"
onmouseover="setAttribute('title',' &quot;link 2&quot; a var???')">link 2</a>



Even better would be if any repeated onclick and onmouseover stuff could be in the header. Thank you for any help you can offer.

sad69
04-27-2004, 08:16 PM
What you want to do is encapsulate the code in a function.

Notice that your code differs only slightly from link to link, and that this change can be held in a variable.

Something like this:


function openLink(url) {
var w=window.open(url, 'popup', 'scrollbars=1,resizable=1,width=590,height=400,left=20,top=20');
return false;
}

<a href="URL_1" title="link name 1"
onclick="return openLink('URL_1');"
onmouseover="setAttribute('title','&quot;link 1&quot; a var???')">link 1</a>


I'm not sure what else you're trying to do with setAttribute (the a var??? part), but I don't think you can encapsulate that much further.. I could be wrong though..

Hope that helps,
Sadiq.

Roy Sinclair
04-27-2004, 08:16 PM
<script type="text/javascript">
<!--
function link2Popup(linkObj)
{
var w=window.open(linkObj.href, 'popup', 'scrollbars=1,resizable=1,width=590,height=400,lef t=20,top=20');
return false;
}
// -->
</script>
</head>
<body>
<a href="URL_1" title="link name 1"
onclick="return link2Popup(this);" title="&quot;link 1&quot; opens in a popup">link 1</a>

<a href="URL_2" title="link name 2"
onclick="return link2Popup(this);" title="&quot;link 2&quot; opens in a popup">link 2</a>


What I've given you will take the HREF attribute from the current link and open it in a popup but in case someone has javascript disabled the link will open in the current window. There's no reason at all for using the onmouseover event to set a title on a link since the title is a standard property.

llizard
04-27-2004, 10:17 PM
Thank you, sad69, that coding is working very well. Also, thanks to you, Roy Sinclair for your reply.

As for the mouseover stuff, I am trying to ensure that the popup links will all open in the same window if people don't close the popups. But as you pointed out, Roy, it would not be advisable to have a new window opening for those who have javascript disabled. I realize that target= is not really allowed for anything but frames but it does seem to work as expected right now.



There's no reason at all for using the onmouseover event to set a title on a link since the title is a standard property.

The reason that I wanted a separate title for the javascript version is that I wanted to have "opens in a popup" for viewers with javascript enabled but not on the title for people with javascript disabled.

I was hoping to do something similar with function setAttribute() (if I could have such a thing) because so much of that part is similar as well. What I envisioned was having only to fill in the relevant parts on the links. Obviously the following is wrong but this is as close as I can come to attempt to explain what I hope to do.



var message=opens in a popup window
[...]
</script>
</head>
<body>
<a href="URL_1" title="link name 1"
onclick="return openLink('URL_1');"
onmouseover="return opensetAttribute('&quot;link 1&quot; + 'message')">link 1</a>

<a href="URL_2" title="link name 2"
onclick="return openLink('URL_2');"
onmouseover="return opensetAttribute('&quot;link 2&quot; + 'message')">link 2</a>


Please accept my apologies if what I'm asking is impossible.

glenngv
04-28-2004, 05:29 AM
function opensetAttribute(objLink){
var addStr = " opens in a popup window";
if (objLink.title.indexOf(addStr)==-1) {//don't add on succeeding mouseover
objLink.title = objLink.title + addStr;
}
}
...
<a href="URL_1" title="link name 1"
onclick="return openLink(this.href);"
onmouseover="opensetAttribute(this)">link 1</a>

<a href="URL_2" title="link name 2"
onclick="return openLink(this.href);"
onmouseover="opensetAttribute(this)">link 2</a>

llizard
04-28-2004, 07:08 AM
This is wonderful, glenngv! It works like a charm.

Thank you very much to all of you for the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum