• 0

javascript variables


Question

hey, how can i get the variable from my javascript inclusion into my .js file?

this is the includsion from my php file

&lt;script type="text/javascript" src="./jscripts/script.js?url=http://google.com"></script>

and this is where i want to use it in the .js

http.open('getb]url[/b]+'files/parse/ajax.php?action='+action

how can i do it? im guessing i need to set the variable somewhere, but im not sure how.

Link to comment
https://www.neowin.net/forum/topic/571477-javascript-variables/
Share on other sites

11 answers to this question

Recommended Posts

  • 0

if you are trying to grab the url from the script tag, you could make it the first script tag on your page and do something like this:

		&lt;script src="myscript.js?url=http://lol.com" charset="utf-8"&gt;&lt;/script&gt;
		&lt;script charset="utf-8"&gt;
			var script = document.getElementsByTagName('script')[0];
			var url = script.src.substring(script.src.indexOf("?url=")+5script.sscript.src.length);
			alert(url);
		&lt;/script&gt;

That should grab it...or you could set your script to have an ID and get it that way. In that case, you would just give your script tag an ID and then grab it with

var script = document.getElementById('theid');

Hope that helped a bit.

  • 0

Or, you could simply have a page such like:

&lt;script type="text/javascript" src="/mypage.aspx?var1=a&var2=b"></script>

You could then get 'mypage.aspx' (or whatever scripting language you use) to write the javascript with a content type of text/javascript.

  • 0

I'd recommend keeping the JS client-side, just declaring the vars before linking to an external js file.

&lt;script type="text/javascript"&gt;
var msg = 'It worked';
&lt;/script&gt;
&lt;script type="text/javascript" src="a.js"&gt;&lt;/script&gt;

Then just reference the var as if it had been declared in the external JS file.

  • 0
  Kudos said:
To those of you suggesting server-side solutions, have fun with the cache!

Hmm...

ASP.NET:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

ASP:

Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1

PHP:

header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

  • 0
  Antaris said:
Hmm...

ASP.NET:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

ASP:

Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1

PHP:

header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

I'm not disputing it's viability. It introduces more complexity that will in the end just make things more difficult than it needs be. As a rule, I avoid messing with the cache unless there is no alternative. In this particular case it is not only bad practice, but completely needless.

Edited by Kudos
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Source: Reuters Clearly, not everyone cares about its CEO's shenanigans. 
    • Sgt Klinger just sounds wrong. I know he made sergeant before end of show, but will always know him as Corporal Klinger. Very sad to hear of Loretta's passing. She definitely helped make the show what it was. 
    • For some reason, Neowin has now pulled this disclaimer pointing out that this survey is utterly useless since it doesn't track the same user data over time (even though Valve has the real information, of course). What it really tracks, without revealing it, is what games are popular each month, who is too busy to answer a dumb survey from month to month, and which cultures (e.g. the Chinese are primarily on expired W10 machines) are gaming in force from wave to wave on whatever game is in vogue at the time. Therefore, these specific surveys, being voluntary and non-consistent, remain nothing but worthless clickbait garbage. They tell us nothing of useful substance regarding actual trends over time.
    • For some reason, Neowin has now pulled this disclaimer pointing out that this survey is utterly useless since it doesn't track the same user data over time (even though Valve has the real information, of course). What it really tracks, without revealing it, is what games are popular each month, who is too busy to answer a dumb survey from month to month, and which cultures (e.g. the Chinese are primarily on expired W10 machines) are gaming in force from wave to wave on whatever game is in vogue at the time. Therefore, these specific surveys, being voluntary and non-consistent, remain nothing but worthless clickbait garbage. They tell us nothing of useful substance regarding actual trends over time.
    • Microsoft will not prevent Windows 11 24H2 or 25H2 or 26H2 from stopping working on those who do not have NPU. This is something very specific to those who want to use Copilot+, only for content creators. Windows 11 works on all hardware released since 2019. It's been 6 years. Any 6-year-old PC has full support for Windows 11. NPU is already standard on mobile phone CPUs and will now be a new standard on PC and notebook CPUs.
  • Recent Achievements

    • Week One Done
      Epaminombas earned a badge
      Week One Done
    • Week One Done
      Prestige Podiatry Care earned a badge
      Week One Done
    • Week One Done
      rollconults earned a badge
      Week One Done
    • One Month Later
      lilred1938 earned a badge
      One Month Later
    • Week One Done
      lilred1938 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      132
    2. 2
      Xenon
      131
    3. 3
      ATLien_0
      123
    4. 4
      +Edouard
      103
    5. 5
      snowy owl
      94
  • Tell a friend

    Love Neowin? Tell a friend!