2

From an external resource I get URLs for trackers that I need to execute/load in the DOM, but a URL with a # in it, gets cut off after the pound sign, whatever I try. I have no control over these URLs as they come from a 3rd party, I want to be able to load this URL without any modification to it. An example URL: http://www.example.com/resource.js?param1=testvalue#a=b&c=d&f=g&js=.js
When executed this URL changes into: http://www.example.com/resource.js?param1=testvalue

The code that I'm sharing works in all cases, except for the URL above.

Original code:

$('#myElement').append('<SCRIPT language="JavaScript1.1" SRC="' + url + '"></SCRIPT>')

What I tried too:

var s = document.createElement("script")
s.type = "text/javascript"
s.src = url
$('#myElement')[0].appendChild(s)

And other variations with appending the child, I tried to insert it as an image element too, but that didn't work either.

The only other post I could find on this suggests that the browser does this (jQuery Ajax Call on URL with Pound (Number) Sign )...seems logical, but the company we're not the first client of this company...

Tim Wachter
  • 732
  • 5
  • 22
  • So you want to pass "#" as parameter? – Dinca Adrian Jul 26 '17 at 08:51
  • It comes down to that indeed, I just want to be able to execute that URL without doing any modifications to it. – Tim Wachter Jul 26 '17 at 08:53
  • 1
    Special characters in the query string need to be escaped. Fragment identifiers (anything after `#`) are not sent to the server by the browser. https://en.wikipedia.org/wiki/Percent-encoding – DarthJDG Jul 26 '17 at 08:57
  • 1
    Possible duplicate of [jQuery Ajax Call on URL with Pound (Number) Sign](https://stackoverflow.com/questions/29753914/jquery-ajax-call-on-url-with-pound-number-sign) – DarthJDG Jul 26 '17 at 09:00

1 Answers1

1

So what you need is to encode the url so you don't let the browser interpret the raw parameters. You can have a look on this post Encode URL in JavaScript? . And maybe check a little bit the documentation to understand why you need to do that.(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent). So you jsut have to redirect to encodeURIComponent(url) or when you add the parameters (link + encodeURIComponent(params)).

Dinca Adrian
  • 1,190
  • 11
  • 21
  • Thanks, this indeed would have fixed it. After some more back and forth the 3rd party was OK with replacing the # with a &, which before according to them wouldn't work.....I asked if we could stop working with clients and customers all together, but something about money... ;) – Tim Wachter Jul 26 '17 at 14:01