• 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

    • Right Click on My Computer Icon -> Properties(?)
    • HomeBank 5.9.2 by Razvan Serea HomeBank is a free software (as in "free speech" and also as in "free beer") that will assist you to manage your personal accounting. It is designed to easy to use and be able to analyse your personal finance and budget in detail using powerful filtering tools and beautiful charts. If you are looking for a completely free and easy application to manage your personal accounting, budget, finance then HomeBank should be the software of choice. HomeBank also benefits of more than 19 years of user experience and feedback, and is translated by its users in around 56 languages. Highlights: Cross platform, supports GNU/Linux, Microsoft Windows, Mac OS X Import easily from Intuit Quicken, Microsoft Money or other software Import bank account statements (OFX, QIF, CSV, QFX) Duplicate transaction detection Automatic cheque numbering Various account types : Bank, Cash, Asset, Credit card, Liability Scheduled transaction Category split Internal transfer Month/Annual budget Dynamic powerful reports with charts Automatic category/payee assignment Vehicule cost HomeBank 5.9.2 fixes: bugfix: #2112135 scheduled with limitation don't stop and goes erroneous data bugfix: #2111663 variable scope issue in ui-assign.c bugfix: #2111359 remind status not synced in transfers Download: HomeBank 5.9.2 | 20.2 MB (Open Source) Download: 3rd party packages (macOSX. Ubuntu...etc) View: HomeBank Website | Support | Features | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • How long until the Windows 11 hating bots come and say that Windows 11 committed atrocities against their family and their dog?
    • If you never consented to cookies (required), and you're blocking ad domains and you never see ads on Neowin (despite whitelisting) wouldn't you agree the adblock popup notice is doing what it is supposed to do?
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      400
    2. 2
      +FloatingFatMan
      178
    3. 3
      snowy owl
      171
    4. 4
      ATLien_0
      169
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!