简体   繁体   English

Javascript window.open两次调用

[英]Javascript window.open Calling Twice

I'm using this code in a php conditional if/else statement to open a new window. 我在php条件if / else语句中使用此代码来打开新窗口。

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). 它打开的是2个窗口而不是一个,而是仅在我的活动站点(专用服务器上)而不是测试站点(共享服务器)上。

EDIT: From zebediah49's suggestion, I appended a random int variable after the new window url. 编辑:根据zebediah49的建议,我在新窗口URL之后附加了一个随机的int变量。 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. 我可能应该提到,它使用的是Joomla和第3方社区应用程序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. 好吧,当我编辑此文件时,在打开同一窗口的〜5分钟后,它打开了window.open窗口的全新实例。 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. 感谢您提供有关语言和最佳做法的提示。

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&r=$rando\");

Also, if you want to avoid having quote issues, I would advise using HEREDOCs: 另外,如果您想避免出现报价问题,我建议您使用HEREDOC:

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. 我想不出一个很好的理由,为什么单行JS会发生两次,这就是为什么我想用random var看到的原因,除了打开两次之外,别无选择。 (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) (如果它在文件中的其他位置,则不会有&r = ...;如果是同一件事被执行两次,则&r = ...在两者之间将有所不同)

First of all. 首先。 Why those in your Javascript ? 为什么您的Javascript中有那些? Those are XML Comment. 这些是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 如果这是对window.open()的唯一调用,则您的http://www.example.com/index.php可能还会进行另一个window.open()调用

可能是PHP回声被触发了两次?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM