www.webdeveloper.com
Results 1 to 3 of 3

Thread: Passing variable to a second web page

  1. #1
    Join Date
    Feb 2012
    Location
    St. Augustine Florida
    Posts
    2

    Passing variable to a second web page

    I am just getting into Javascript and I am stuck on a real simple task.

    What I want to do is pass the filename of a picture in Page1 to Page2 and display the picture on Page2. ASP, PHP, CSS are well beyond me.

    In Page1, I have a hyperlink for Page2 as:

    <a href="Page2.html?picturefilename.jpg">click here</a>

    In Page 2:

    1. Using Javascript, how do I create a variable containing picturefilename.jpg ?

    2. How do I use the variable in an "<IMG" statement to show the picture?

    Thanks for your help!

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    First, give the filename an associated variable name. Instead of "Page2.html?picturefilename.jpg", use "Page2.html?imgName=picturefilename.jpg".

    Next, take a look at the following - nice little function that might help.
    http://papermashup.com/read-url-get-...ithjavascript/

    In the area where the picture is supposed to show on Page2.html, create a div and give it an ID, whatever you want to call it. I'll call it "imageHere".

    Copy/paste the function from the above link in the bottom of Page2.html (just before the closing /body tag); immediately after it, write code that will use the function to grab the value of imgName and set it to a variable (call the variable "imageName").

    Then use DOM to put the <img> tag inside the "imageHere" div.

    Code:
    document.getElementById("imageHere").innerHTML = '<img src="' + imageName + '">';

  3. #3
    Join Date
    Feb 2012
    Location
    St. Augustine Florida
    Posts
    2

    Got it working!

    Hey WolfShade:

    Many, many thanks for your help!!

    I got it working ... after many tries ... due to missing one character in the function.

    I really do appreciate your solution ... I struggled most of yesterday with the problem due to my very limited knowledge if Javascript.

    Not only did I get it working but I learn quite a bit in the process. Loved the use of the Alert to test it out.

    My first impression of Javascript is that it can be very powerful but very difficult to test out. That part was just plain trial and error.

    Thanks again!

    Peter White
    Saint Augustine, Florida

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles