SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    echo $domain in javascript

    hi

    i want to replace "http://localhost/site" with "$domain" variable value

    PHP Code:
    echo "<script language='javascript'>";
    echo 
    "window.location='http://localhost/site/shop/'";
    echo 
    "</script>"
    vineet

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yes, and the problem is?

    PHP Code:
    echo "<script language='javascript'>"
    echo 
    "window.location='$domain/shop/'"
    echo 
    "</script>"
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi scallio

    $domain already contains a slash / in the end.

    so i get two slash after domain //

    PHP Code:
    <?
    $domain 
    "http://localhost/mysite/";
    echo 
    "<script language='javascript'>"
    echo 
    "window.location='$domain/shop/'"
    echo 
    "</script>"
    ?>
    vineet

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Ah, so then it would become $domainshop which of course doesn't work. To avoid this you can use curly brackets to tell PHP where the var is delimited, like this:

    PHP Code:
    <?
    $domain 
    "http://localhost/mysite/";
    echo 
    "<script language='javascript'>"
    echo 
    "window.location='{$domain}shop/'"
    echo 
    "</script>"
    ?>
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    great scallio

    it works fine.

    i learned a new thing about using curly brackets this way

    vineet

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Are you just using this to redirect btw?

    If so, this would be better:

    PHP Code:
    $domain "http://localhost/mysite/";
    header("Location: {$domain}shop/"true301);
    exit; 
    That way PHP will redirect and the user doesn't need to have javascript enabled, plus Google likes this whereas it doesn't understand the javascript redirection.

    Just saying
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi scallio

    i used javascript redirect because i cannot echo anything before header() php

    vineet

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yeah okay that makes sense
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •