• 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

    • Yeah, I totally get your point, which is possible it could happen. I just hope there is a few people around him who are similar to where if they took over things would run pretty much the same. if not, then yeah, it could start to decline rapidly etc. but I figure something that's been around for a longer period of time with a decent backing, and probably more users than most Linux distro's (which I would 'imagine' Mint is one of the more used Linux desktop distro's by volume of people who use it), is less likely to just disappear. but like you said, nothing is guaranteed. but I do think you are probably right in that Clem is probably the core of what keeps Mint, Mint. I like how it tends to stay pretty much the same with some slight tweaks here and there (but is largely the same) instead of that crap some people go for with change for the sake of change trying to create a overly fancy interface and other unnecessary stuff etc. I also feel Mint keeps a nice balance of things out-of-the-box where it's not too bloated, nor too striped down. p.s. but I see Mint as a better Ubuntu basically. but I get your point like if it was more of a really serious choice of needing a 'safe bet' to use long term, then yeah something like official Ubuntu would be one of the better choices for sure given what you said with it being backed by an actual company which makes it a safer bet than Mint which is smaller and 'could' potentially be more fragile.
    • It’s in development so hopefully it’s improved upon before release.
    • For the 10th iteration they could have bothered with some design changes rather than just carrying over the 9's
    • So this requires hardware to be setup in the area you want to listen to music in? And it requires hardware for each user? So useless for me when I'm out walking my dog? Or when I'm at the gym and want to listen to my podcasts rather than the music the gym is broadcasting? Heck, will it even work in my own home and allow me to walk around from room to room, or even within a room? Yeah, these won't be replacing headphones anytime soon.
  • Recent Achievements

    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
    • One Month Later
      Ian_ earned a badge
      One Month Later
    • Dedicated
      MacDaddyAz earned a badge
      Dedicated
    • Explorer
      cekicen went up a rank
      Explorer
  • Popular Contributors

    1. 1
      +primortal
      504
    2. 2
      ATLien_0
      211
    3. 3
      Michael Scrip
      200
    4. 4
      Xenon
      147
    5. 5
      +FloatingFatMan
      122
  • Tell a friend

    Love Neowin? Tell a friend!