Results 1 to 5 of 5

Thread: Change Title

  1. #1
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Question Change Title

    Hi

    I'm looking for a script that will change the title element on my page depending on what the title is in a iframe (on the same page). For example, if the title of the page in the iframe is 'My Awesome Page' than the title of the main (parent) page is 'My Awesome Page'. Anyone know how?
    Last edited by kaos; 11-03-2009 at 12:58 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Rather simple, example:

    HTML Code:
    <iframe src="another.htm" onload="document.title=this.contentDocument? this.contentDocument.title : this.contentWindow.document.title;" width="300" height="300" scrolling="auto" frameborder="1"></iframe>
    One caveat though, both pages must be on the same domain.

    A consideration - iframes are in the process of being deprecated. So a different approach altogether for managing your site and its content is warranted.

    see also:

    http://www.dyn-web.com/tutorials/iframes/
    Last edited by jscheuer1; 10-31-2009 at 05:12 AM. Reason: add link for more info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Yeah, thats cool and all, but the page in the iframe is constantly changing... I need the title to update every time it changes.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Generally, it should. The onload attribute is supposed to fire each time a page is loaded into the iframe. In most browsers it will.

    However, it is possible, since as I say iframe is in the process of being deprecated, that some browsers may not fire this event when assigned in this manner.

    If you are experiencing this problem, the event may also be assigned via attach/add, and that may (probably will) take care of it, ex:

    Code:
    <iframe id="myIframe" src="another.htm" onload="document.title=this.contentDocument? this.contentDocument.title : this.contentWindow.document.title;" width="300" height="300" scrolling="auto" frameborder="1"></iframe>
    <script type="text/javascript">
    (function(el){
    	function loadFunc(e){
    		e = e || window.event;
    		var f = e.target || e.srcElement, fb = f.contentDocument || f.contentWindow.document;
    		document.title = fb.title;
    	}
    	if (window.addEventListener)
    		el.addEventListener('load', loadFunc, false);
    	else if (window.attachEvent)
    		el.attachEvent('onload', loadFunc);
    })(document.getElementById('myIframe'));
    </script>
    Still, as I also mentioned before, all of the pages involved MUST BE ON THE SAME DOMAIN. There is no cross page communication across domains in javascript without certification, which is a bit complex and requires a certificate from a third party. These certificates are difficult to obtain, expire periodically and must be renewed, so this is not something to enter into lightly. I don't recommend it unless the benefits would outweigh the costs involved.

    But if all of your pages are on the same domain, certs are not an issue, and need not be used.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Thanks

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
  •