Get query string value in 3rd party javascript

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 909 Views - Last Post: 24 June 2013 - 07:10 PM

#1 travesty  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 67
  • Joined: 04-June 13

Get query string value in 3rd party javascript

Posted 23 June 2013 - 11:14 PM

I've researched on how to do this for the last 2 or 3 hours. I haven't been able to come up with a solution to this yet. I did get something that worked but not when it was done 3rd party. What I want to do is get query string values using 3rd party javascript. So one site would have this:
<script type="text/javascript" src="example.com/example.js?var=1"></script>


Then I want the 3rd party javascript (example.js) to be able to get what "var" is in the script src.

I'm sorry if the question is bad, I just haven't been able to come up with a solution on my own after hours of searching.

Is This A Good Question/Topic? 0
  • +

Replies To: Get query string value in 3rd party javascript

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Get query string value in 3rd party javascript

Posted 23 June 2013 - 11:21 PM

you could access the src property of the <script> element and get everything after the "?".
Was This Post Helpful? 0
  • +
  • -

#3 travesty  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 67
  • Joined: 04-June 13

Re: Get query string value in 3rd party javascript

Posted 23 June 2013 - 11:34 PM

View PostDormilich, on 23 June 2013 - 11:21 PM, said:

you could access the src property of the <script> element and get everything after the "?".

I'm sorry, I'm a beginner, but would you know how to go about doing that?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Get query string value in 3rd party javascript

Posted 23 June 2013 - 11:50 PM

it depends upon how much access you have to the client HTML (or how much you know of it).
Was This Post Helpful? 0
  • +
  • -

#5 travesty  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 67
  • Joined: 04-June 13

Re: Get query string value in 3rd party javascript

Posted 23 June 2013 - 11:53 PM

View PostDormilich, on 23 June 2013 - 11:50 PM, said:

it depends upon how much access you have to the client HTML (or how much you know of it).

I have no access to the client html. I'll create a program to generate the code for them to copy and paste on their site.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 01:57 AM

if you create the HTML code to include, you can give the <script> an ID and then you can access your URL via
var url = document.getElementById(...).src;

Alternately (if that is still too error prone) you can get all <script> tags via document.getElementsByTagName() and then check each script’s src for your script name (e.g. via String.contains())
Was This Post Helpful? 0
  • +
  • -

#7 travesty  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 67
  • Joined: 04-June 13

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 01:15 PM

View PostDormilich, on 24 June 2013 - 01:57 AM, said:

if you create the HTML code to include, you can give the <script> an ID and then you can access your URL via
var url = document.getElementById(...).src;

Alternately (if that is still too error prone) you can get all <script> tags via document.getElementsByTagName() and then check each script’s src for your script name (e.g. via String.contains())

This also worked 1st party but not 3rd party. Could it be something with my code?
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>

<script type="text/javascript" id="test" src="show_ads.js?p=1&c=1&t=v"></script>


</body>
</html>


3rd Party Script:
var url = document.getElementById("test").src;
document.write(url);



And this is ALL the code.
Was This Post Helpful? 0
  • +
  • -

#8 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,829
  • Joined: 30-April 10

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 02:01 PM

Calling a query string in the src of the js file will cause a 404 error as it is unable to find the name in the directory because you would have to name the file accordingly. For example:

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>

<script type="text/javascript" id="test" src="show_ads.js?p=1&c=1&t=v"></script>

<script type="text/javascript"src="3rdparty.js"></script>
</body>
</html>



The first script named test would not be found unless it was named show_ads.js?p=1&c=1&t=v because it looks for the literal name. The script with the source of 3rdparty.js can be found and will get the src of test.

3rdparty.js
var url = document.getElementById("test").src;
console.log("From 3rd party" + url);

ads.js
console.log("from ads" + document.getElementById("test").src);



This produced this in the console
GET http://localhost/dic/show_ads.js?p=1&c=1&t=v 404 (Not Found) localhost/:8
From 3rd partyhttp://localhost/dic/show_ads.js?p=1&c=1&t=v 3rdparty.js:2


Was This Post Helpful? 0
  • +
  • -

#9 travesty  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 67
  • Joined: 04-June 13

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 02:09 PM

View PostDormilich, on 24 June 2013 - 01:57 AM, said:

if you create the HTML code to include, you can give the <script> an ID and then you can access your URL via
var url = document.getElementById(...).src;

Alternately (if that is still too error prone) you can get all <script> tags via document.getElementsByTagName() and then check each script’s src for your script name (e.g. via String.contains())

Honestly I have no idea why it wasn't working, it didn't do anything when I just put "document.write("hi");" in it, but when I put my code into a new Javascript file named "test.js" and changed the url in the script src it actually did something. Then I tried this "Passing Javascript arguments via the src attribute", but it returns "undefined". So I'll keep working at it. Thanks for your help so far!
Was This Post Helpful? 0
  • +
  • -

#10 travesty  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 67
  • Joined: 04-June 13

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 03:28 PM

okay, for those who are also wondering how to do this, "Passing Javascript arguments via the src attribute", worked. For some reason I wasn't able to get it to work when executing with "localhost" instead "file:///C:/xampp/htdocs/". I'm just going to start testing everything from now on an actual server, which should hopefully fix that (a free web host of course).
Was This Post Helpful? 0
  • +
  • -

#11 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,829
  • Joined: 30-April 10

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 03:35 PM

You could also look into a local host server like xamppor wampp. These contain a server (apache) that can be used to test on you local computer.
Was This Post Helpful? 0
  • +
  • -

#12 travesty  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 67
  • Joined: 04-June 13

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 03:42 PM

View Postlaytonsdad, on 24 June 2013 - 03:35 PM, said:

You could also look into a local host server like xamppor wampp. These contain a server (apache) that can be used to test on you local computer.

Actually, I already use XAMPP.
Was This Post Helpful? 0
  • +
  • -

#13 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,829
  • Joined: 30-April 10

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 03:49 PM

:oops:

I test all my javascript and html via xampp so as I don't have an issue with external files.
Was This Post Helpful? 0
  • +
  • -

#14 travesty  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 67
  • Joined: 04-June 13

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 03:54 PM

Usually I test all my stuff with xampp too, but for some reason it isn't working this time, and I was having a problem with the name of the 3rd party file too. It didn't do ANYTHING using "show_ad.js", not even a simple "document.write("Hi");", but when I renamed it to "test.js", it worked. Then using query strings returned undefined though. The only thing that woked was launching it not with xampp on localhost.
Was This Post Helpful? 0
  • +
  • -

#15 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,829
  • Joined: 30-April 10

Re: Get query string value in 3rd party javascript

Posted 24 June 2013 - 04:14 PM

Use the developers console in the browser and make sure it is loading the pages you think it is.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2