Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics Programming AJAX Programming AJAX Challenge

Kurt Coleman
Kurt Coleman
2,967 Points

Passing variables into Javascript by URL

It's not in this video, but I'm having a problem passing variables in a javascript file by URL. Currently I have this:

<script src='/js/main.js?chapter=27&numpages=3'></script>

I want to invoke /js/main.js and pass two variables into the script. I have both variables assigned values in the script. I want to override the values declared in the script with the passed variables. I don't know how to parse the passed variables in /js/main.js.

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I assume you're trying to send data using AJAX via POST.

I think it's a problem with trying to invoke it directly in the HTML rather than in your script. It needs to be invoked in the script rather than something your markup does.

Try adding the query strings to the end of your URL in the browser.

Kurt Coleman
Kurt Coleman
2,967 Points

Jonathan,

Thanks for your reply. I am invoking the program using this notation:

<script src='/js/main.js?chapter="14"&numpages="10"'></script>

It is within the script main.js that I am having problems parsing the call.