[英]Passing URL with query parameters as function parameter in JavaScript
I have a URL that looks like我有一个看起来像的网址
https://go.feeds4.com/merchants/?pid=1676&mid=4261
How do I pass this as a function parameter.我如何将它作为函数参数传递。 I am trying to pass the value but not able to get anything in the function.我正在尝试传递值,但无法在函数中获取任何内容。
Does the &
in query paramter cause issues? &
in 查询参数是否会导致问题?
HTML HTML
<a href="javascript:;"
onclick=rendernewmodal(<?php echo '"'.htmlspecialchars_decode($merchant->affiliate_link).'"'; ?>);>
<i class="fa fa-angle-right" aria-hidden="true"></i>
</a>
On View Page Source, I can see the following在查看页面源代码上,我可以看到以下内容
<a href="javascript:;"
onclick=rendernewmodal("https://go.feeds4.com/merchants/?pid=1676&mid=4261");>
JS JS
function rendernewmodal(url) {
alert (url);
}
I don't see any alert showing value of the URL.我没有看到任何显示 URL 值的警报。 Please help.请帮忙。
You have two problems:你有两个问题:
Mashing strings together to make JavaScript literals is error prone and messy, so you should avoid doing that by hand too.将字符串混合在一起以生成 JavaScript 文字很容易出错且混乱,因此您也应该避免手动执行此操作。
So:所以:
<?php
# Get the URL
$url = $merchant->affiliate_link;
# Put quotes around it and escape special characters for JS
$url_as_js_string_literal = json_encode($url);
# Put it in the rest of the JS
$js_function_call = "rendernewmodal($url_as_js_string_literal);";
# Escape special characters for HTML
$html_safe_js = htmlspecialchars($js_function_call);
?>
onclick="<?php echo $html_safe_js; ?>)">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.