1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Call Javascript function from <TITLE>?

Discussion in 'HTML & Website Design' started by johnwerry, Jul 6, 2007.

  1. #1
    Can I do something like the code (which doesn't work) below? I want to return a value from a javascript function to be the title (see the attempted call in the TITLE tag below). How can I tweak this to work?

    Second question is how do search engines read a site? Will they see the executed function value and index it like that or do they read sites statically (function never executed)? In other words would they index my site with a title of "hi" or a title of "returndata()" based on the code below?

    Thanks,
    John

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html; charset=utf-8">
    <META content="MSHTML 6.00.6000.16481" name=GENERATOR>
    <STYLE type=text/css>
    BODY{font-family:verdana, arial, sans-serif}
    </STYLE>

    <SCRIPT language=JavaScript type=TEXT/JAVASCRIPT>
    <!--

    function returndata()
    {
    return "hi";
    }

    //-->
    <TITLE>returndata()</TITLE>;
    </SCRIPT>
    </HEAD>
    <BODY></BODY></HTML>
     
    johnwerry, Jul 6, 2007 IP
  2. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do this in your body tag:

    <body onload = "returndata();">
    Code (markup):
     
    rgchris, Jul 6, 2007 IP
  3. johnwerry

    johnwerry Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    All that does is call a function and does nothing with the result. It does not populate <TITLE> as the <HEAD> section is long gone at that point. Thanks for the thought...
     
    johnwerry, Jul 6, 2007 IP
  4. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh, that's what you're trying to do? Change the title in the browser? If that's what you want, try this:

    <script>
    function testfunction()
    {
    	document.title = "Hi, this is my title";
    }
    </script>
    
    <html>
    	<title></title>
    	<body onload = "testfunction();">
    	<body>
    </html>
    Code (markup):
    Just change the text in the string to what you want and you can also call it using a different event if you want.
     
    rgchris, Jul 6, 2007 IP
  5. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Why are you trying to use JavaScript to dynamically write a page title?

    (By the way, that script is better called using an event handler attached to the window object rather than in the <body> tag.)
     
    Dan Schulz, Jul 6, 2007 IP
  6. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Meh. It was just quick mock up to get the general idea across.
     
    rgchris, Jul 6, 2007 IP
  7. johnwerry

    johnwerry Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry, forgot to mention that I don't want to use document.title. I want to have a value in <title> tag itself. Google isn't indexing with my document.title tag I've dynamically set so I need to set <title>.
     
    johnwerry, Jul 7, 2007 IP
  8. jawanda

    jawanda Peon

    Messages:
    151
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    johnwerry,

    It's funny that people are giving you such comprehensive answers on the one hand without mentioning the other, much more relevant part of your question: NO, Google will NOT execute your javascript and thus this will not have the desired effect. You need to either do it serverside (php,asp,etc) or assume it is just for your users and not for the se's.

    Good luck,

    -Phil
     
    jawanda, Jul 7, 2007 IP
  9. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Actually Phil, I wasn't neglecting the other part of his question. I wanted to make him think about it first. I find it helps the brain learn. :)
     
    Dan Schulz, Jul 7, 2007 IP
  10. johnwerry

    johnwerry Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    OK, well this doesn't work:

    <title>
    <?php
    echo "Testing php title";
    ?>
    </title>
     
    johnwerry, Jul 9, 2007 IP
  11. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Your server probably doesn't support PHP then. There is a quick and easy test for this however. Open a new file, write the following in it, save it as php-info.php and upload it to your server. Then look at the file in a browser (like you would a regular Web page).
    
    <?php php_info(); ?>
    
    PHP:
    Don't put ANYTHING ELSE into the file. Not a DOCTYPE, not an HTML tag, NOTHING. Just what I told you to write.

    If all you see is that line of code, then your server doesn't support PHP. If you see a LOT of information (presented in a purplish table), then it does.

    You'll also want to put that file into a password protected folder since it contains VERY sensitive information about your server's configuration.
     
    Dan Schulz, Jul 9, 2007 IP
  12. johnwerry

    johnwerry Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I get something different than either of your scenarios:

    Fatal error: Call to undefined function: php_info() in /home/content/r/a/r/obfuscated/html/php-info.php on line 1
     
    johnwerry, Jul 9, 2007 IP
  13. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You might have a botched PHP install then.
     
    Dan Schulz, Jul 9, 2007 IP