SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    always learning . . .
    Join Date
    Nov 2003
    Location
    UK
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help in getting variable by URL

    how do i get the variable at the end of a URL so i can use it?

    script.html?srcPic=001.jpg

    HOW i get the 001.jpg?

    so can use it as the src image?

    I pass the variable to the end ok i think

    THANKS,

  2. #2
    SitePoint Member rich_explus's Avatar
    Join Date
    Nov 2003
    Location
    Fort Collins CO
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a relatively lame solution that only works if there's only one variable in the URL query string.
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    	var urlstring = location.href;
    	alert(urlstring);
    	var qrystring = urlstring.substring(urlstring.indexOf("=") + 1, urlstring.length);
    	alert(qrystring);
    	document.write("<IMG SRC='" + qrystring + "'>");
    
    // -->
    </SCRIPT>
    Playing with indexOf() and substring would enable you to handle more than one variable.
    Hope this helps,
    --Rich

  3. #3
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    or eval(this.location.href.split("?")[1])
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's a version that would work if there's multiple variables in the query string...

    Code:
    function getQuery(name){
    	var query = self.location.search;
    	if(query.length > 0){
    		name = name+"=";
    		var str = query.substring(1);
    		var start = str.indexOf(name);
    		if(start == -1) return null;
    		start += name.length;
    		var end = str.indexOf("&", start);
    		if(end == -1) end = str.length;
    		return unescape(str.substring(start,end));
    	}else{
    		return null;
    	}
    }
    
    function getImageTag(src){
    	return '<img src="'+src+'" />';
    }
    
    document.write(getImageTag(getQuery("srcPic")));

  5. #5
    always learning . . .
    Join Date
    Nov 2003
    Location
    UK
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    only one variable! cheers guys


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •