简体   繁体   English

使用window.open()传递与号

[英]Passing an ampersand using window.open()

I'm trying to perform this function: 我正在尝试执行此功能:

var form = window.open(www.example.com/?param1=' + value1 + '&param2 =' + value2 ,'1_blank', params); var form = window.open(www.example.com/?param1='+ value1 +'&param2 ='+ value2,'1_blank',params);

But the problem is, that's opening a new window with the url of: 但是问题是,这将打开一个新的窗口,其URL为:

www.example.com/?param1=XXX#038;param2=YYY www.example.com/?param1=XXX#038;param2=YYY

While I need it to open with the url of: 虽然我需要用以下网址打开它:

www.example.com/?param1=XXX&param2=YYY www.example.com/?param1=XXX&param2=YYY

How do I force the ampersand to stay as an ampersand symbol and not get changed to the unicode label? 如何强制与符号保留为“&”符号而不更改为unicode标签?

I'm trying to do this in WordPress, by the way, and am not sure if this is a Wordpress problem is a JS problem. 顺便说一句,我正在尝试在WordPress中执行此操作,并且不确定这是否是Wordpress问题还是JS问题。

Using window.open with an ampersand sign works fine for me. 使用带有符号号的window.open对我来说很好用。 I think the problem you're having is some special character in your value1 and value2 . 我认为您遇到的问题是value1value2中有一些特殊字符。

So use encodeURIComponent . 因此,请使用encodeURIComponent It's built into JavaScript. 它内置在JavaScript中。 Use it like this: 像这样使用它:

var form = window.open(www.example.com/?param1=' + encodeURIComponent(value1) + '&param2=' + encodeURIComponent(value2),'1_blank', params);

Just a note: you had a space (" ") after &param2 and right before the equals ("=") sign. 请注意:在&param2之后&param2等号(“ =”)之前,您有一个空格(“”)。 This would cause the parameter not to be sent over. 这将导致不发送该参数。

This is a strange problem, so I'm just throwing out an idea. 这是一个奇怪的问题,所以我只是提出一个想法。 It might just be a browser problem. 这可能只是浏览器问题。

If you are placing your Javascript in the content of your page/post, then Wordpress runs the text through a filter called 'wptexturize'. 如果要将Javascript放在页面/帖子的内容中,则Wordpress将通过名为“ wptexturize”的过滤器运行文本。 This would account for your ampersands being replaced. 这将说明您的“&”号被替换了。

If that is the case, you can remove the filter from your theme's functions.php file: 在这种情况下,您可以从主题的functions.php文件中删除过滤器:

remove_filter( 'the_content', 'wptexturize' );

I hope that helps. 希望对您有所帮助。

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

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