Results 1 to 5 of 5

Thread: Javascript Question?

  1. #1
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Javascript Question?

    I have this webpage that uses <input type='text' id='userInput'
    and a Iframe, with the user input I want them to change the the 'URL' thats inside the iframe, the default Iframe link looks like this

    <script type="text/javascript">
    function changeIFRAMETONEWURL(){

    <!--- the code here??
    }
    </script>


    <input type='text' id='userInput' value="http://www.w3schools.com/TAGS/tag_iframe.asp"/>


    <input type='button' onclick='changeIFRAMETONEWURL()' value='Change Url'/>


    I tried at runtime this piece of code.

    <input type='text' id='userInput' value="http://www.w3schools.com/TAGS/tag_iframe.asp"/>

    the Iframe would not display the url located in <input type='text' id='userInput' 'Textbox'
    <iframe
    src ='userInput' <!---- this does not work -->
    width="100%">
    </iframe>

    and I tried 'still nothing?
    <iframe
    src ="userInput.value" <!--- does not work also -->
    width="100%">
    </iframe>

    using the javascript how can I change the iframe using the Userinput -URL they
    enter.
    Thank You Ben

  2. #2
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    FOREWARNING: I HAVE NOT TESTED THIS!
    I am basing this all on logic, so I am sorry if this does not work. However, I think it will help you get the right idea or maybe even a new way to think of it. Hope it helps:
    Code:
    <script language="javascript">
    function changeIFRAMETONEWURL(elem, elem2)
    {
    var string = document.getElementById(elem);
    var frame = document.getElementById(elem2);
    
    frame.src = string.value;
    
    }
    </script>
    
    <iframe
    src ='userInput' id="frm"
    width="100%">
    </iframe>
    
    <input type='text' id='userInput' value="http://www.w3schools.com/TAGS/tag_iframe.asp"/>
    <input type='button' onclick='javascript:changeIFRAMETONEWURL(userInput, frm);' value='Change Url'/>

  3. The Following User Says Thank You to Moshambi For This Useful Post:

    orlandojobz (09-20-2008)

  4. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<title>Untitled Document</title>
    		<style type="text/css">
    
    		</style>
    		<script type="text/javascript">
    		window.onload = function(){
    			document.forms['f1'].elements['load'].onclick = function(){
    				changeIfrSrc(document.forms['f1'].elements['url'].value,"ifr");
    			}
    		}
    		function changeIfrSrc(url,ifr){
    			if(url.indexOf('http://') == -1){
    				url = 'http://' + url;
    			}			
    			document.getElementById(ifr).src = url;
    		}
    		</script>
    	</head>
    	<body>
    		<form name="f1">
    			<label>Url</label>
    			<input type="text" name="url" value="http://www.w3schools.com/TAGS/tag_iframe.asp"/>
    			<br/>
    			<input type="button" value="Load" name="load"/>
    		</form><br/>
    		<iframe id="ifr" frameborder="0" width="900" height="300" src=""></iframe>		
    	</body>
    </html>

  5. The Following User Says Thank You to codeexploiter For This Useful Post:

    orlandojobz (09-20-2008)

  6. #4
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Javascript Question?


    Thank You Moshambi I like to keep your Idea.
    Thanks codeexploiter It worked!
    "codeexploiter"
    I have a question?
    Can I make the Iframe Appear visible before the user input.
    I' am sure I need to do something to the iframe property, but I need to play around with
    that, or do a google on the iframe visibility.
    Thanks again.

  7. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Can I make the Iframe Appear visible before the user input.
    Yes you can do that. Moreover you can have a default page in your iframe. So nothing else specified this default site will be loaded.

    But before using iframe you should understand that the iframe's has its own issues. Sometimes it will act different in different browsers especially if you try to maintain the iframe dynamically.

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
  •