tags:

views:

50

answers:

3

I'm using this code in a php conditional if/else statement to open a new window.

echo("<script type=\"text/javascript\">
<!--
window.open(\"http://www.example.com/index.php?$var\");
//-->
</script>
");

It's opening 2 windows instead of one, but only on my live site (on a dedicated server) and not on the test site (shared server).

EDIT: From zebediah49's suggestion, I appended a random int variable after the new window url. It changes the variable with each new window instance, so I assume that means that it runs the conditional twice. I probably should mention that it's using Joomla and a 3rd-party community app, JomSocial.

Alright, as I was editing this and after ~5min of the same window being open, it opened a brand new instance of the window.open window. So obviously it automatically refreshes once as soon as it lands. I'll have to pry into that. Still any suggestions are welcome.

Thank you for your tips regarding language and best practices.

A: 

First of all. Why those in your Javascript ? Those are XML Comment. You need to use /* ... */ or //

Did you mean

//<![CDATA[
...code...
//]]>

If this is the only call to window.open() your http://www.example.com/index.php might also make another window.open() call

This is the only call and the url that window.open() is executing is unique. It's opening the same unique url twice.Not sure about the issue with the comments, although I did leave out // in this post. Fixed that now.
Marc Ripley
No, he wants XML comments. It's a somewhat old thing to do, but it comments out that line if the browser doesn't understand <script> tags. It's not supposed to comment out the JS.
zebediah49
+1  A: 

Since it's performing differently, there is either something different in the code between the two, or something different in the environment.

To test to be very, very, very sure that it is opening that specific url twice, have it append a random variable to the end of the URL:

$rando = rand();
window.open(\"http://www.example.com/index.php?$var&amp;r=$rando\");

Also, if you want to avoid having quote issues, I would advise using HEREDOCs:

echo(<<<EOF<script type="text/javascript">
<!--
window.open("http://www.example.com/index.php?$var");
//-->
</script>
EOF
);

or with a variable

$content = <<<EOF<script type="text/javascript">
<!--
window.open("http://www.example.com/index.php?$var");
//-->
</script>
EOF;
echo($content);

I can't think of a good reason why that single line of JS would happen twice though, which is why I would want to see with the random var, such that there is no possible way other than that it could get opened twice. (If it was somewhere else in the file, it would not have the &r=...; if it was the same thing getting executed twice, &r=... will be different between the two)

zebediah49
These are good suggestions. The $rando is changing with both new window instances. That must mean that it's looping through the conditional twice for some reason, right? It's not repeating the following conditional as that would create additional html on the original page. I'll post some additional info in the OP. Also, I went with string concatenation since I'm more familiar with it.
Marc Ripley
A: 

Could it be that the PHP echo is being triggered twice?

George Marian