在源网址中插入自定义值

时间:2016-08-14 16:15:07

标签: javascript html escaping

如何转义src url以插入自定义值?我的目标:

<script src="https://url.com?value=MY_ESCAPED_CUSTOM_VALUE"></script>

2 个答案:

答案 0 :(得分:0)

试试这个:

    var x=document.querySelector("[src^='https://url.com']");
    var url=x.getAttribute('src');
    var value=url.substring(url.lastIndexOf('='),url.length-1);
    url=url.replace(value,YOUR_CUSTOM_VALUE);
    x.setAttribute("src", url);

答案 1 :(得分:0)

var url="https://some.url?value=" + MY_ESCAPED_CUSTOM_VALUE;
<script src={url}></script>