Yoda
Yoda

Reputation: 339

Appending window.location in src

I'm trying to append window.location.path name to source and currently it is not returning the value. Am i appending it correctly?

   <script src="abc.com/gethistory?product=aj1&m=abn&lang='"+ window.location.pathname.substring(1,2)" ></script>

Upvotes: 0

Views: 712

Answers (2)

Dan Starns
Dan Starns

Reputation: 3815

Your script is not inside any other javascript code, window is a javascript object and only accessible inside a javascript script. If you want to achieve the desired outcome you can write the following script.

const script = document.createElement("script");
script.src = `abc.com/gethistory?product=aj1&m=abn&lang="${window.location.pathname.substring(1,2)}"`;

document.head.appendChild(script);

Upvotes: 1

Barmar
Barmar

Reputation: 781761

JavaScript isn't executed in HTML tags. You need to write a script that creates the tag with the computed URL.

var script = document.createElement("script");
script.src = "abc.com/gethistory?product=aj1&m=abn&lang="+ window.location.pathname.substring(1,2)";
document.head.appendChild(script);

Upvotes: 2

Related Questions