Passing URL with query parameters as function parameter in JavaScript -
i have url looks like
https://go.feeds4.com/merchants/?pid=1676&mid=4261
how pass function parameter. trying pass value not able in function.
does &
in query paramter cause issues?
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, can see following
<a href="javascript:;" onclick=rendernewmodal("https://go.feeds4.com/merchants/?pid=1676&mid=4261");>
js
function rendernewmodal(url) { alert (url); }
i don't see alert showing value of url. please help.
you have 2 problems:
- when putting data html document, have encode html not decode from html
- attribute values need quoting when contain characters (like quotes)
mashing strings make javascript literals error prone , messy, should avoid doing hand too.
so:
<?php # url $url = $merchant->affiliate_link; # put quotes around , escape special characters js $url_as_js_string_literal = json_encode($url); # put in rest of js $js_function_call = "rendernewmodal($url_as_js_string_literal);"; # escape special characters html $html_safe_js = htmlspecialchars($js_function_call); ?> onclick="<?php echo $html_safe_js; ?>)">
Comments
Post a Comment