Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2004
    Location
    N of San Antonio TX
    Posts
    9,151
    Plugin Contributions
    11

    Default Can I run a <script> based on the URL?

    This is sort of a 301 redirect question.

    I have one site using two different URLs. I do not want the 301 to change the browser's URL. IOW, when the user arrives at site A, they see the URL for site A. When they arrive at site B, the URL in the browser stiill shows site B but they are on site A for data/db purposes.

    The problem is that a script needs to be run that is coded based on the URL being used.

    includes/templates/YOUR_TEMPLATE/common/tpl_main_page.php contains the pre-<body> code and scripts for the site.

    The question is, how can I tell the site:

    If URL = A
    <script>lfaslfkjslfkasljf</script>
    else
    <script>wrpowpowp'wro</script>

    That would run the second script if the user came from site B's URL.

    THANX for any thoughts. Not finding much in Stack

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,491
    Plugin Contributions
    88

    Default Re: Can I run a <script> based on the URL?

    Making an assumption that the site will either be 'A' (in my example, this site!) or 'B', in that file, you'll code something like
    PHP Code:
    <?php
    if ($_SERVER['SERVER_NAME'] === 'www.zen-cart.com') {
    ?>
    <script>lfaslfkjslfkasljf</script>
    <?php
    } else {
    ?>
    <script>wrpowpowp'wro</script>
    <?php
    }
    Note that there's no https:// preceding the site's server name. See also this (https://stackoverflow.com/questions/...er-name-in-php) link regarding the differences between $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'].

  3. #3
    Join Date
    Jan 2004
    Location
    N of San Antonio TX
    Posts
    9,151
    Plugin Contributions
    11

    Default Re: Can I run a <script> based on the URL?

    Of course, it works like a charm when the closing ?> is added at the end. I noticed because I'm from the era where all files had to have the closing tag.
    Code:
    <?php
    if ($_SERVER['SERVER_NAME'] === 'www.zen-cart.com') {
    ?>
    <script>lfaslfkjslfkasljf</script>
    <?php
    } else {
    ?>
    <script>wrpowpowp'wro</script>
    <?php
    }
    ?>
    Great choice on SERVER_NAME.

    THANX for your help and all you do for us here. Hope you and yours are doing well after Ian

 

 

Similar Threads

  1. v155 How to run script from admin as CRON
    By DarkMen in forum Basic Configuration
    Replies: 16
    Last Post: 24 Feb 2018, 05:06 AM
  2. Replies: 3
    Last Post: 25 Sep 2013, 10:11 PM
  3. How to run a script upon purchase?
    By djex in forum General Questions
    Replies: 2
    Last Post: 6 Feb 2009, 05:26 PM
  4. I can't run the installer
    By tmoholland in forum Installing on a Windows Server
    Replies: 8
    Last Post: 27 Aug 2008, 01:54 AM

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
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR