-1

I have a function that loads more database results. This function is called on different pages so i have given its own file and then linked to it in each of those pages e.g.:

<script type="text/javascript" src="js/loadmorebuilds.js">
</script>

In that file, i run an ajax call to a file and use a segment of the pages URL.

If i create that segment in the file itself it doesnt work as im guessing it uses the URL of the page. My work around currently is like so:

<script>
  var exampleSegment = window.location;
</script>
<script type="text/javascript" src="js/loadmorebuilds.js">
</script>

However i am writing a longer method of getting that segment from the URL and want to include it in with the js file itself so i dont have to go editing it on every page that i need it.

Is there a way to do this? Thanks, hope it makes sense...

Lovelock
  • 7,689
  • 19
  • 86
  • 186
  • Hope you're doing some security checks on that `exampleSegment` variable. –  May 01 '14 at 20:14

1 Answers1

0

So what you're asking is how you can get the url of the external script to go from there instead? If the script is loaded with the page, it will be the last on the page as of the script running. So you can use a code like:

var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length - 1];
alert(lastScript.src);

Source is another post on this forum Can code in a Javascript file know its own domain/URL

Community
  • 1
  • 1
tomysshadow
  • 870
  • 1
  • 8
  • 23