Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help getting a variable from a URL

Status
Not open for further replies.

scottjo1

MIS
Oct 7, 2003
5
US
I just started managing an online courseware product for my company. url:


anyway, in this online course there are several lessons, each lesson has its own XML file defining what slides to load, etc... i'd like to be able to define what lesson is played first via the URL such as
adl.html?lesson=1

I'm new to javascript, how do I go about getting this variable and evaluating it? the main flash movie is callled
citpoParentFlashMovie.html so currently it would embed

citpoParentFlashMovie.html?ADL.xml

how to I make that change to the following via the url

citpoParentFlashMovie.html?Lesson1.xml
citpoParentFlashMovie.html?Lesson2.xml etc...

Thanks a lot, e-mail if you'd like:

jonathan.scott@ngc.com
 

to extract pageNumber you would use a function similar to this, and

myValue = getValue('embed');

In this example, it will split up the URL into pairs and cycle through until it matches a value with embed.

function getValue(match){
var a = window.location.href.split('?')[1];
a = a.split('&');
var b = new Array();
var i=0;
while (i!=a.length){
b = a.split('=')[1];
a = a.split('=')[0];
if (a==match) return b;
i++;
}
}

Having said all that, I'm not the best solution provider on this board :(

----------
I'm willing to trade custom scripts for... [see profile]
 
function getValue(match){
var a = window.location.href.split('?')[1];
a = a.split('&');
var b = new Array();
var c=0;
while (c!=a.length){
b[c] = a.split('=')[1];
a[c] = a.split('=')[0];
if (a[c]==match) return b[c];
c++;
}
}

Sorry, board chopped out some of the characters!

----------
I'm willing to trade custom scripts for... [see profile]
 
Even that won't work because of a goof. And now look, I'm spamming the thread! :[

function getValue(match){
var a = window.location.href.split('?')[1];
a = a.split('&');
var b = new Array();
var d = new Array();
var c=0;
while (c!=a.length){
b[c] = a.split('=')[1];
d[c] = a.split('=')[0];
if (d[c]==match) return b[c];
c++;
}
}

----------
I'm willing to trade custom scripts for... [see profile]
 
hey, thanks for the reply, So now that I can use that script to evaluate the url, do I just use "IF" statements so that it follows the correct link?

if (c == 2) {embed.... lesson 2}
if (C == 1) {embed .... lesson 1}

sorry, i'm new to javascript, been reading the books but havn't been able to figure this one out yet.
the link below is the actual html file i'll be putting it into...


at the bottom you will see where it loads adl.xml how do i change that to the proper lesson xml file?
 
Sorry, there was another character chopped out by the board.

function getValue(match){
var a = window.location.href.split('?')[1];
a = a.split('&');
var b = new Array();
var c=0;
while (c!=a.length){
b[c] = a[c].split('=')[1];
a[c] = a[c].split('=')[0];
if (a[c]==match) return b[c];
c++;
}
}

---

You would use something like

<script>
document.write('<EMBED src=&quot;.swf?xmlToLoad='+getValue('xmlToLoad')+'></EMBED>);
</script>

But now I feel really stupid because the next solution is much quicker and just passes the data through without parsing it - so no oportunity for my shoddy code to mess things up.

<script>
var a = window.location.href.split('?')[1];
document.write('<EMBED src=&quot;.swf?'+a+'></EMBED>');
</script>



----------
I'm willing to trade custom scripts for... [see profile]
 
awesome, thanks a lot for the help, I'll go play with this for a bit and see what happens.
 
if i use document.write won't that just change the html file, will the HTML file reload to run the script?
 
document.write() is added while the page is being rendered, just like rendering the next HTML element.

<IMG SRC=&quot;&quot;>
<SCRIPT>document.write('<IMG SRC=&quot;&quot;>');</SCRIPT>
<IMG SRC=&quot;&quot;>

Assuming Javascript is not disables, this creates 3 images in exactly the same as having 3 IMG tags lined up.

----------
I'm willing to trade custom scripts for... [see profile]
 
scottjo1 - javascript has a built in variable for getting the querystring from the url.

it is location.search

if you use this, it might be helpful if you are going to be running your scripts from an asp or php or simmilar generated page.

your value would then be retrieved easiest this way:

Code:
var movieValue = location.search.substring(1);

you could then make the html tag:
Code:
document.write('<EMBED src=&quot;.swf?'+movieValue+'&quot;></embed>');


stormbind & scottjo-
the document.write method only makes a new page if the method is called after the page is loaded. As stormbind pointed out, if it is called inline, as opposed to calling it in a function after the page is loaded, the document.write method does inlay its output into the html page.


Also, check this out. you might want to check out the &quot;process TGML&quot; link at the bottom of the reply box. You can easily prevent the forum program from nicking your code with alittle bit of TGML.


Robert Carpenter
/b{2}|!b{2}/ - that is the question...
robert@convertingchaos.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top