Forum Moderators: open

Message Too Old, No Replies

problem concatenating

         

optik

8:55 am on Jun 2, 2009 (gmt 0)

10+ Year Member



The following code is not working properly, at least not in safari.
From some reason the width attribute is not being set when the window is opened, I can't figure out why?

function moreInfo(val1,val2,val3,val4){

var windowWidth="width=";
var windowHeight="height=";
var comma=",";
var quote="'";
var windowAttr=",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no";

var attributes=quote.concat(windowWidth).concat(val3).concat(comma).concat(windowHeight).
concat(val4).concat(comma).concat(windowAttr).concat(quote);

window.open(val1,val2,attributes);

}

[edited by: whoisgregg at 7:24 pm (utc) on June 3, 2009]
[edit reason] fixed sidescroll. :) [/edit]

daveginorge

9:34 am on Jun 2, 2009 (gmt 0)

10+ Year Member



Your syntax is incorrect
[w3schools.com...]

Try
attributes=quote.concat(windowWidth, val3, comma, windowHeight, val4, comma, windowAttr, quote);

daveVk

10:47 am on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either syntax looks Ok to me, or simply use "+"
attributes=quote+windowWidth+val3...

place alert(attributes) prior to open to check if Ok.

optik

7:05 pm on Jun 2, 2009 (gmt 0)

10+ Year Member



the syntax is fine as I can alert the attributes variable and it looks fine

rocknbil

11:20 pm on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From some reason the width attribute is not being set when the window is opened

Is there a reason you're using concat?
Also, you don't need "yes" "no" "1" or "0" for window attributes; just include them if you want them, leave them out if you don't. It might even be this that's giving you the grief.

Second, the first parameter, window name, is meant to be a unique id. If you just pass "windowname" to it every time, visualize this scenario:

User clicks link, window opens.
User clicks back to main window.
User clicks another link, and the content *DOES* load in the already-open window.
But since the already open window is BEHIND the main window, their impression? Your site is broken.

For that reason I've eliminated "val1" and set the ID using a time method, which guarantees the content will *always* open in a new window (if it's not blocked.) Along the same line of thinking, it's always good to use variables that describe what they do instead of anonymous arbitrary names - hence val2=url, val3=w(idth), val4=h(eight).

Simplifying your code might make life easier . . . see if this works.


function moreInfo(url,w,h){
var day=new Date();
var id=day.getTime();
var attributes='width='+w+',height='+h;
//Leaving off resizable and scrollbars is a *really bad idea.*
//You never know your user's environment. So you might want this instead:
//var attributes='width='+w+',height='+h+',resizable,scrollbars';
window.open(id,url,attributes);
}

optik

4:31 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



using the + notation has solved it thanks