0

How to escape src url to insert custom value? My targer:

<script src="https://url.com?value=MY_ESCAPED_CUSTOM_VALUE"></script>
IntoTheDeep
  • 4,027
  • 15
  • 39
  • 84
  • 3
    This is a duplicate of the StackOverflow question here: [Encode URL in JavaScript]: http://stackoverflow.com/questions/332872/encode-url-in-javascript – Alex Aug 14 '16 at 16:26

2 Answers2

0

Try this :

    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);
Nihar Sarkar
  • 1,187
  • 1
  • 13
  • 26
0
var url="https://some.url?value=" + MY_ESCAPED_CUSTOM_VALUE;
<script src={url}></script>
IntoTheDeep
  • 4,027
  • 15
  • 39
  • 84
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 15 '16 at 01:59