#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Pass iframe id php to javascript


    Hi is there a way to pass iframe id ... php to javascript

    in php my code
    PHP Code:
    echo "<iframe name='messageTxt' id ='" $e "' src='" $e "' style='display:block'></iframe><input type='button' value='Import' onClick='init()' />"
    Produce:

    Code:
    <iframe name='messageTxt' id ='../resumes/20130412092633.txt' src='../resumes/20130412092633.txt' style='display:block'></iframe><input type='button' value='Import' onClick='init()' /><iframe name='messageTxt' id ='../resumes/20130412111328.txt' src='../resumes/20130412111328.txt' style='display:block'></iframe><input type='button' value='Import' onClick='init()' />
    So as you could see the iframe name is the same but id is changing...I want to onClick be able to load specific txt file in the same iframe on the page

    my javascript code is

    Code:
    <script type="text/javascript">	
    	function init(){
    		var extText = window.frames.messageTxt.document.body.lastChild.lastChild.data;
    		var firstLine = extText.split('\n')[0];
    		var secondLine = extText.split('\n')[2];
    		var forthLine = extText.split('\n')[3];
    		document.forms[0].newstext.value = forthLine;  
    		document.forms[0].title.value = firstLine;
    		document.forms[0].name.value = secondLine;	
    	}
    </script>
    As i see it must be something with
    Code:
    window.frames.messageTxt.document.body.lastChild.lastChild.data;
    Thanks for the help
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    Is this what your trying to do?
    Code:
    <script>
    function getContent(getID)
    {
    var framey = document.getElementById(getID);
    framey = framey.contentWindow ? framey.contentWindow.document : framey.contentDocument;
    var extText = framey.body.innerHTML.toLowerCase().replace("<pre>","");
    extText = extText.replace("</pre>","");
    var firstLine = extText.split('\n')[0];
    var secondLine = extText.split('\n')[1];
    var forthLine = extText.split('\n')[3];
    document.forms[0].newstext.value = forthLine;  
    document.forms[0].title.value = firstLine;
    document.forms[0].name.value = secondLine;
    }
    </script>
    
    <iframe id="myFrame" src="../resumes/20130412092633.txt"></iframe>
    <iframe id="myFrame2" src="../resumes/20130412111328.txt"></iframe>
    
    <form action="#" onsubmit="return false">
    <input type="text" name="newstext"/><br/>
    <input type="text" name="title"/><br/>
    <input type="text" name="name"/><br/><br/>
    <input type="button" value="Get Iframe 1 Content" onclick="getContent('myFrame')"/>
    <input type="button" value="Get Iframe 2 Content" onclick="getContent('myFrame2')"/>
    <input type="reset"/>
    </form>
    This should get the 3 lines from your "*.txt" file(s) and display the text content (from that line) in a text input field.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Yes but the things is that i don't know the frame id no do i know how txt files are in...

    in php i retrive the files with

    PHP Code:
    <iframe name='messageTxt' id ='" . $e . "' src='" . $e . "' style='display:block'></iframe><input type='button' value='Import' onClick='init()' />"; 
    so in javascript i would like to do something like

    Code:
    var extText = window.frames.messageTxt.document.body.getElementById('<?php echo($e); ?>');
    is there a way to do that...that last code not working???


    thanks
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    Originally Posted by jfmichaud
    is there a way to do that...that last code not working???
    Actually, it does work... I tested it before I posted it.
    Originally Posted by jfmichaud
    Yes but the things is that i don't know the frame id no do i know how txt files are in...
    Well..., if you insert the id dynamically (with PHP), into the getContent() "getID" parameter; then it will not matter, if you know the iframe id or txt file location or not. It still will display the content, from inside the iframe (as long as the iframed .txt file(s) resides within your domain).
    Originally Posted by jfmichaud
    so in javascript i would like to do something like
    Code:
    var extText = window.frames.messageTxt.document.body.getElementById('<?php echo($e); ?>');
    If your using a button (to trigger this function); then you do not need to statically embed a specific id (of an iframe) within the function; it's already set-up to dynamically (using a JavaScript parameter, in a function) get the iframe id (with the "getID" parameter of the getContent() function).
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Thanks that pretty much what i want...

    Yeah your code's working

    I was talking mine not working :P

    Code:
    <pre style="word-wrap: break-word; white-space: pre-wrap;">
    Why do i have that in the first section?


    I'll will try to put that on my web site to show you what i mean and want you're code is pretty much but need a little twist...

    Thanks again
  10. #6
  11. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    The <pre> tag is just automatically parsed in there by the browser; although I am not sure why a browser would be adding CSS, to that parsed tag. In the example I provided you, I replaced the opening and closing <pre> tag(s); so you would just get plain text. Glad it's working out for you; good luck with the twist.

IMN logo majestic logo threadwatch logo seochat tools logo