Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 43
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Can I change the title bar of my site every X seconds to a new title?

    This page: http://homepage.ntlworld.com/kayseyc...document2.html
    seems to indicate that it's possible, but it looks like it also pops up an alert window which would be annoying as hell. Is there a way to, say, once every minute change the title bar text?

    You see I have a text file of rotating slogans that appear each time you reload a page on my site and someone suggested that having them change even when the page is already open might be fun. I thought I'd look into it and see if it's possible. The site is http://opalcat.com.

    I don't know enough about javascript to know if this is possible. I am fairly good at implementing and installing other people's code, though, and can usually make minor changes to tweak things.

    Please let me know if this is a doable thing, and if so, if you'd be willing to help me work it out. Thanks in advance,

    OpalCat

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Just don't use alert( ). Nobody said you *had* to do so.

    Code:
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    var delay = 3; // number of seconds
    var titles = [ "This is my first title",
                   "Any second now...",
                   "Third time is a charm",
                   "What comes fourth now comes forth",
                   "About now I need a fifth"
                  ];
    
    var curtitle = 0;
    function nextTitle()
    {
        document.title = titles[curtitle];
        curtitle = ( curtitle + 1 ) % titles.length;
        setTimeout( nextTitle, delay * 1000 );
    }
    nextTitle();
    </script>
    </head>
    <body>
    body
    </body>
    </html>
    Try it.

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Well the file I have has hundreds. I don't want to load them all into the script... is there a way to have it pull from a file at random?

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This sounds like exactly what I want, but pulling from a text file. Maybe someone awake during the day knows the answer?

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Just don't use alert( ). Nobody said you *had* to do so.
    (Just so you know, I don't know javascript well enough to know how to remove that without asking. ::hangs head in shame:: )
    Last edited by OpalCat; 08-08-2011 at 05:52 PM. Reason: stupid smiley showed up!

  • #6
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Quote Originally Posted by OpalCat View Post
    This sounds like exactly what I want, but pulling from a text file. Maybe someone awake during the day knows the answer?
    I think the question should be, How are you accomplishing this now? If it is also via JavaScript we can probably help you adapt the code.

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Right now I'm using a plugin for textpattern that calls random lines from a text file

    Right now my code looks like this:

    Code:
    <title><txp:dru_random_text source="file" path="/home/jugglerd/public_html/opalcat/random_titles.txt" /><txp:page_title /></title>

  • #8
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    And the source of the plugin is here:

    Code:
    	function dru_random_text($atts) // by mr drew mclellan // allinthehead.com
        {
            global $txpcfg;
            if (is_array($atts)) extract($atts);   
            $source     = empty($source)    ? 'file'    : $source;
            
            if ($source == 'database'){  
                if (empty($table) || empty($column)) return '';
                $sql    = "select $column from $table order by rand() limit 1";
                $rs     = getRow($sql);
                if($rs) return $rs[0];
                return '';
            }
            
            if ($source == 'file'){
                $path       = empty($path)  ? $txpcfg['doc_root'].'/random.txt' : $path;
                $delimiter  = empty($delimiter) ? "\n"  : $delimiter;
                
                $fp         = fopen($path, "r");
                $contents   = fread($fp, filesize($path)); 
                $elements   = explode($delimiter,$contents); 
                fclose($fp); 
                srand((double)microtime()*1000000); 
                $text_index = (rand(1, sizeof($elements)) - 1); 
                return $elements[$text_index];             
            }
            
            return '';
        }

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    You could use JS to read the text file easily enough.

    But if it's JUST a text file with lines of titles, converting it to a JavaScript in an array would be trivial with any decent text editor.

    Just add a " to the front of each line and a ", to the end of each line, which you should be able to do with REPLACE ALL if the editor is smarter than, say, Notepad. And then add the stuff to the front to make the array, zap the last comma and tack on ]; and voila, you can then dump it into your HTML file.

    However, if you insist on reading the text file from JS, then you must first make sure it is coming from the SAME DOMAIN as the page using it. That is, if your HTML page is, say, "http://www.mysite.com/somepath/xyz.html" then the text file must also be accessible via "http://www.mysite.com/any/path/filename.txt".

  • #10
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    They're definitely on the same domain. I really don't want hundreds of lines of title code clogging up my HTML though. How can I make it read from the file?

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    It will be more efficient to put it in the HTML, honest. It may make the HTML file larger, but the page will load a smidgen faster. Especially because you will have to wait for JavaScript to read in the file before it can randomly pick a line for the title. Sure you don't want to reconsider that choice?

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Here...this seems to work:
    Code:
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    var url = "mytitles.txt"; // use relative path/to/your/textfile.txt
    var delay = 3; // number of seconds
    
    var xml;
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xml = new XMLHttpRequest();
    } else {
        xml = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xml.open( "GET", url, false );
    xml.send( );
    var titles = xml.responseText.split("\n");
    
    var curtitle = 0;
    function nextTitle()
    {
        var ttl = "";
        while ( ttl.length < 3 ) /* 3 is arbitrary...just guarding against blank lines */
        {
            ttl = titles[curtitle];
            curtitle = ( curtitle + 1 ) % titles.length;
        }
        document.title = ttl;
        setTimeout( nextTitle, delay * 1000 );
    }
    if ( titles.length > 3 ) nextTitle();  // 3 arbitrary here, as well
    </script>
    </head>
    <body>
    body
    </body>
    </html>
    If you prefer that the titles be chosen randomly, instead of rotating through the list, that would be a trivial change.

  • #13
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    It will be more efficient to put it in the HTML, honest. It may make the HTML file larger, but the page will load a smidgen faster. Especially because you will have to wait for JavaScript to read in the file before it can randomly pick a line for the title. Sure you don't want to reconsider that choice?
    Well I also don't want people to look at the source code and get to see them all.

  • #14
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Here...this seems to work:

    If you prefer that the titles be chosen randomly, instead of rotating through the list, that would be a trivial change.
    Random would be better--what would the change be?

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Just change the function plus the one line right above it. The rest stays the same:
    Code:
    var curtitle = -1;
    function nextTitle()
    {
        var ttl = "";
        var tnum = curtitle;
        while ( tnum == curtitle || ttl.length < 3 ) 
        {
            tnum = Math.floor( Math.random() * titles.length )
            ttl = titles[tnum];
        }
        curtitle = tnum; 
        document.title = ttl;
        setTimeout( nextTitle, delay * 1000 );
    }
    That is written to ensure the same title won't appear twice in a row, but it *is* possible (though unlikley) that you might see something like title#17, title#223, title#17 (that is, a repeat soon after).

    If you need to ensure that all titles are shown before starting over, that's more code, but not that much more.


  •  
    Page 1 of 3 123 LastLast

    Tags for this Thread

    Posting Permissions

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