Using a variable in the src attribute of iframe

Discussion in 'NZ Computing' started by Dany P. Wu, Nov 17, 2003.

  1. Dany P. Wu

    Dany P. Wu Guest

    Okay, last time I asked a question about HTML I just about got my head
    bitten off. Anyway, I'm game enough to try again in hope I'd get some useful
    comments.

    I've got a couple of pages which has a variable passed between them using
    Javascript.

    The opening page of the website has a bunch of links that look somewhat
    like:

    <a href="geninfo.html?sub_contact.html">

    The page geninfo.html contains an inline frame called "frame" and has a
    short script like this:

    <!--
    var temp=window.location.split("?");
    var content=temp[1]
    //-->

    I am assuming after I clicked the link from the opening page the variable
    "content" now contains "sub_contact.html". Am I correct?

    How can I use this variable in my iframe tag?

    I've tried this:

    <iframe name="frame" height="409" width="580" border="0" frameborder="0"
    src=content scrolling="auto">

    but that's obviously wrong since it didn't work.

    Can someone please tell me how I can achieve this? I have searched the web
    but none of them really show how I can achieve this. They have a lot of
    examples setting values of various controls on different frames of the same
    page, but not inline frames. This has really got me confused. Some
    assistance would be greatly appreciated.

    Cheers,
    Dany.
     
    Dany P. Wu, Nov 17, 2003
    #1
    1. Advertisements

  2. Dany P. Wu

    Jay Guest

    Your 'src=content' isn't going to be interpreted as javascript, is it?
    How is the HTML interpreter to know what it is?

    I'm not sure of the exact answer but something like src=javascript:content
    or via a javascript function like src="javascript:eek:penit('content')" is
    going to have a better chance of working.
     
    Jay, Nov 17, 2003
    #2
    1. Advertisements

  3. Dany P. Wu

    rob Guest

    Hi Dany,

    don't assume that I actually know what I'm talking about but....
    I reckon you may need to use Document.write (javascript) to combine the HTML
    and the javascript variable.
    So you might have something like this:

    <iframe name="frame" height="409" width="580" border="0" frameborder="0"
    scrolling="auto"

    <script language="JavaScript">
    <!--
    document.write "src=" & content & ">"
    //-->
    </script>

    I expect I've got some syntax wrong etc. but you get the idea.

    If you want to speak to some pretty knowledgeable people you could try the
    pviiwebdev newsgroup on forums.projectseven.com
    I have a wee lurk around there from time to time - they're a pretty friendly
    bunch.
    have fun
    rgds
    rob
     
    rob, Nov 17, 2003
    #3
  4. Dany P. Wu

    rob Guest

    by the way, I'd shy away from using frame as a name - it maybe a reserved
    word or some such.
     
    rob, Nov 17, 2003
    #4
  5. Dany P. Wu

    Mainlander Guest

    Straight HTML contains nothing at all to do with variables. Your example
    would need to be rewritten to use Javascript to output the frame name in
    the place where you have shown the variable name.
     
    Mainlander, Nov 17, 2003
    #5
  6. Dany P. Wu

    Dany P. Wu Guest

    Thanks Rob, I figured it out when I got out of bed this morning and you're
    right, your syntax is wrong but the general idea of parsing the variable
    into the HTML tag is right on the spot!
    Thanks for that too.

    Cheers,
    Dany.
     
    Dany P. Wu, Nov 18, 2003
    #6
  7. You need to use Dynamic HTML. That is to say, by making assignments to
    appropriate objects within the document, you can selectively replace
    content. Here's a quick-and-dirty working example I just threw together:

    <HTML>
    <HEAD><TITLE>IFRAME Test</TITLE>
    <BODY>
    <P>This text comes before.
    <P><IFRAME NAME="testframe" SRC="http://www.microsoft.com/" WIDTH=400
    HEIGHT=400>
    <P>This text comes after.
    <BUTTON ONCLICK="document.testframe.location =
    'http://www.apple.com'">Click Me</BUTTON>
    </BODY>
    </HTML>

    This initially loads up showing the Microsoft home page within the
    iframe. Click the "Click Me" button, and this is changed to the Apple
    home page.

    Anyway, that should give you the idea.
     
    Lawrence D¹Oliveiro, Nov 18, 2003
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.