SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict ewiz's Avatar
    Join Date
    Dec 2003
    Location
    Egypt
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.write('<scr' + 'ipt>) issue

    I am using a script that webmasters use in their pages to access a file on my server for calculating hits. It goes like this:
    Code:
    <script type="text/JavaScript" language="JavaScript">
     document.write('<scr' + 'ipt src="http://www.example.com/script.php?referrer=' + escape(document.referrer) + '"></scr' + 'ipt>'); 
     </script>
    I am using this way because in the file which processes the hits this is the only way to detect the referrer (when trying to detect referrer by PHP in the file, it considers the page that is calling a referrer).
    My problem is: In the file, I use PHP's $_SERVER['HTTP_USER_AGENT'] to check the visitor's browser and type. It seems it is not detecting google.. Is that because the small javascript code is written this way? Is there a way around?
    add your hosting company and plans for free
    Ahsan Host
    my business site
    Internet Plus

  2. #2
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Google doesn't process scripts on the page, so as soon as google sees the first <script ... > tag it skips to the corresponding </script> tag.

    A common way around this is to use the <noscript> tag and place some code in there that will enable you to track the visit, whether this is from google, or even a site visitor that has javascript turned off.

    A bit like:

    <script ... >
    // place your existing code in here
    </script>
    <noscript>
    // new code for javascript challenged browsers
    </noscript>


    Mike

  3. #3
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can make that inline script into an external script so you don't have to split '<script>', then see if it works.

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's a problem with that markup. You cannot have the character sequence '</' inside a SCRIPT element, because its content is declared as CDATA rather than (#PCDATA) in the DTD. As soon as the parser encounters the '</', the SCRIPT element is terminated.

    Insert an escaping backslash between the '<' and the '/' (highlighted in red):
    Code:
    <script type="text/JavaScript" language="JavaScript">
     document.write('<scr' + 'ipt src="http://www.example.com/script.php?referrer=' + escape(document.referrer) + '"><\/scr' + 'ipt>'); 
    </script>
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Addict ewiz's Avatar
    Join Date
    Dec 2003
    Location
    Egypt
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot all of you
    add your hosting company and plans for free
    Ahsan Host
    my business site
    Internet Plus

  6. #6
    SitePoint Addict ewiz's Avatar
    Join Date
    Dec 2003
    Location
    Egypt
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK so now I used:
    Code:
     
    <script type="text/JavaScript" language="JavaScript"> document.write('<scr' + 'ipt src="http://www.example.com/script.php?id=2&referrer=' + escape(document.referrer) + '"><\/scr' + 'ipt>'); </script>
    <noscript>
    </noscript>

    But seems like the contents of noscript are not getting performed because it didn't detect googlebot.

    How can I execute script1.php if noscript?
    add your hosting company and plans for free
    Ahsan Host
    my business site
    Internet Plus

  7. #7
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NOSCRIPT can only contain block-level elements (IMG is inline). Also, IMG elements must have an ALT attribute. Finally, attribute values with special characters in them must be enclosed in quotes (a good idea is to always put quotes around all attribute values). So your markup is invalid.

    Try this:
    HTML Code:
    <noscript>
      <p><img src="http://www.example.com/script1.php?id=2" alt=""></p>
    </noscript>
    Birnam wood is come to Dunsinane

  8. #8
    SitePoint Addict ewiz's Avatar
    Join Date
    Dec 2003
    Location
    Egypt
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thansk alot. I will try that.
    add your hosting company and plans for free
    Ahsan Host
    my business site
    Internet Plus

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It won't work. Googlebot doesn't 'download' or 'access' images. Nor, for that matter, will it necessarily access or download any external page components, and while it may index things like iframes and whatnot, you can't be sure it will do that every time it visits the parent page.

  10. #10
    SitePoint Addict ewiz's Avatar
    Join Date
    Dec 2003
    Location
    Egypt
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well.. I am 24 hours far from discovering that. But IMW, do you have any suggestions?
    add your hosting company and plans for free
    Ahsan Host
    my business site
    Internet Plus


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
  •