Page 1 of 2 12 LastLast
Results 1 to 16 of 32

Thread: Javascript Help

  1. #1
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts

    Javascript / PHP Help

    Ok I need I need a javascript script to redirect to a specific page in my site, however the name needs to be got from teh active document kinda like this ....

    if a user has opened www.mydomain.com/test.php

    they automatically should be redirected to www.mydomain.com/index.php?page=test

    Does anyone know how i would go about doing this?

    Thanks
    Last edited by Basher; 29-08-2003 at 07:51 PM.

  2. #2
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,773
    Thanks
    22
    Thanked
    40 times in 24 posts
    You'd be better off doing it at the server side (i.e. in PHP) rather at the client side (i.e. JSscript).
    Simon


  3. #3
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    ok so how would I go about doing it in PHP ?

    Thanks

    Basher
    Last edited by Basher; 29-08-2003 at 07:51 PM.

  4. #4
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,168
    Thanks
    735
    Thanked
    1,607 times in 1,045 posts
    This would do it in PHP. Its all self explantory and commented.

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Agents Forwarding page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <?
    // CONFIGURATION
    // first, set the variables for your doamin name
    $domain "http://www.mydomain.com";
    // set the variable for the page that they are to be forwarded to, INCLUDING the /
    $forwardpage "/index.php";
    // set what you want the name of the variable to be that is past to the page
    $variablename "page";
    // END OF CONFIGURATION

    //Find out the page name, and set it to the variable $pagename
    $pagename $PHP_SELF;
    //Set the string we are looking for so we can get all text before it
    $findstring ".";
    //reverse the string so we can get the extension of the file. This way it will work even if the extension is not .php
    $pagename strrev($pagename);
    //find the "." and remove everything after it
    $result strchr($pagename,$findstring);
    //reverse the string so its back to normal
    $result strrev($result);
    // remove the "." from the string
    $result substr($result0, -1);
    //remove the / from the start of the string
    $result substr($result1);
    ?>
    <script language="JavaScript">location.href="<? echo $domain.$forwardpage."?".$variablename."=".$result ?>";</script>
    <body>
    </body>
    </html>
    You could have the last $result = substr($result, 0, -1); as $result = substr($result, 1, -1); and remove the line before it so it does it in one function. I have done it in 2 seperate functions so you can see what the script is doing clearer.

    Last edited by Agent; 30-08-2003 at 04:12 AM.
    Quote Originally Posted by Saracen View Post
    And by trying to force me to like small pants, they've alienated me.

  5. #5
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    thats brill thank yee

  6. #6
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    Hi it doesn;t seem to be doing quite what i need, when i go to mydomain.com/forward.php it does redirect me to ?page=forward, which is waht I want, but then it redirects me to ?page=index, which I dont want it to do, why is it doing this?

    cheers

  7. #7
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    Just thought, once it redirects to the correct page=forward, it opens teh page again and do runs the script again,

    so what is needed is some kind of

    If page adress contains index?page= tehn dont run that script,

    how would I go about doing that?

    cheers

  8. #8
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,168
    Thanks
    735
    Thanked
    1,607 times in 1,045 posts
    Why is it running the script again, why do you need to include it in the index.php file ?
    Quote Originally Posted by Saracen View Post
    And by trying to force me to like small pants, they've alienated me.

  9. #9
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    Ok if a user goes to www.myspace.com/test.php they are forwarded to where they should be ie.

    www.myspace.com/index.php?page=test so that the template os there.

    which is what the script did, however once they are forwarded there, the page loads again, as it is effectively opening teh page again and so loading the script, and so keeps going round a loop chaging the URL, which is why i'd need an if statement to initially check if the user had gone to the index?page=text page

  10. #10
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    As far as I can tell, the reason it's going to ?page=index is because you have that script up there as index.php as well as test.php, so of course it's going to repeat itself. For infinite, actually. Remove that code from the index.php and you're set.

    Also, if you'd rather not use javascript at all, you can replace the javascript segment with:

    <meta http-equiv="refresh" content="0;<? echo $domain.$forwardpage."?".$variablename."=".$result ?>">

    Move the code above the </head> tag and you're set.

    PS: *sighs as all his HTML codes were effectively rendered visible*
    Last edited by eldren; 30-08-2003 at 07:28 PM.

  11. #11
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    That code isn't in index.php

    no i only used those names as examples, its going to run indefinately unless i put an if statement in checking the URL before the code is executed.

    How would I go about making an if statement in PHP checking if the address contains '?page=' ?

    thanks
    Last edited by Basher; 30-08-2003 at 07:33 PM.

  12. #12
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,168
    Thanks
    735
    Thanked
    1,607 times in 1,045 posts
    Originally posted by Basher
    That code isn't in index.php

    no i only used those names as examples, its going to run indefinately unless i put an if statement in checking the URL before the code is executed
    why ?

    Once the script is called, it forwards to the page you tell it to, with the variable being passed.
    At that point, the script is dead
    Quote Originally Posted by Saracen View Post
    And by trying to force me to like small pants, they've alienated me.

  13. #13
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    no because when it forwards to that page, essentially it is loading the same page again, just with the template (index.php) around it, so as it loads the page again, it will load the script again.

    I've made a file with your script and uploaded it so you can see what i mean

    http://www.asginternational.com/testfw.php

    (you'll prob have to 'stop' the page loading eventually as it just keeps loading the index page)
    Last edited by Basher; 30-08-2003 at 07:37 PM.

  14. #14
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    Eh well. Agent can kick me if I mess this up, seeing as I don't really know PHP just yet.

    ...
    // remove the "." from the string
    $result = substr($result, 0, -1);
    //remove the / from the start of the string
    $result = substr($result, 1);
    // and now, an IF block to check that the page is NOT index
    if ($result != "index") { echo "<script language=\"JavaScript\">location.href=".$domain.$forwardpage."?".$variablename."=".$result.";</script>"; }
    ?>
    <body>
    </body>
    </html>

  15. #15
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,168
    Thanks
    735
    Thanked
    1,607 times in 1,045 posts
    Originally posted by Basher
    no because when it forwards to that page, essentially it is loading the same page again, just with the template (index.php) around it, so as it loads the page again, it will load the script again.

    I've made a file with your script and uploaded it so you can see what i mean

    http://www.asginternational.com/testfw.php

    (you'll prob have to 'stop' the page loading eventually as it just keeps loading the index page)
    ok, ive just looked at it, and your including my script in the index.php file - why ?
    once its done its job of forwarding to the index.php with the variables, you dont need to touch my script again.

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    title>ASG Agronomic Services Group Home</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    script language="JavaScript" type="text/JavaScript">
    <!--
    function 
    MM_reloadPage(init) {  //reloads the window if Nav4 resized
      
    if (init==truewith (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
        
    document.MM_pgW=innerWidthdocument.MM_pgH=innerHeightonresize=MM_reloadPage; }}
      else if (
    innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgHlocation.reload();
    }
    MM_reloadPage(true);
    //-->
    </script>
    <link href="Style.css" rel="stylesheet" type="text/css">
    </head>

    <body vlink="#000000" alink="#006600">
    <div align="center"> <strong><font size="+1" face="Verdana, Arial, Helvetica, sans-serif"> 
      </font></strong> 
      <table width="723" border="0" cellpadding="0" cellspacing="0" bgcolor="cad599">
        <!--DWLayoutTable-->
        <tr> 
          <td height="150" colspan="7" valign="top"><img src="images/MainLogo.jpg" alt="Our Logo" width="723" height="150"></td>
        </tr>
        <tr> 
          <td width="1" rowspan="4" valign="top"><img src="images/SideLine.gif" alt="Border!" width="1" height="660"></td>
          <td width="3" height="287">&nbsp;</td>
          <td width="115" rowspan="2" valign="top"><p><font size="+1" face="Verdana, Arial, Helvetica, sans-serif">Links</font></p>
            <p><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><font color="#000000"><a href="?page=about"> 
              About ASG</a><br>
              <br>
              <a href="?page=SupportPro">Support Programme's</a><br>
              <br>
              <a href="?page=CorpStru">Corporate Structure<br>
              </a><a href="?page=GlobalNetwork"><br>
              Global Network</a><br>
              <br>
              <a href="?page=FeeStructure">Fee Structure</a><br>
              <br>
              <a href="?page=InfoManage">Information Management</a></font></font></p>
            <p><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><a href="?page=search">Search</a><strong><br>
              </strong></font></p></td>
          <td width="1" valign="top"><img src="images/SideLine.gif" width="1" height="287"></td>
          <td width="9"></td>
          <td width="593" rowspan="4" valign="top"> 
            
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Agents Forwarding page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <script language="JavaScript">location.href="http://www.asginternational.com/index.php?page=index";</script>
    <body>
    hi 
    </body>
    </html>

          </td>
          <td width="1" rowspan="4" valign="top"><img src="images/SideLine.gif" alt="Border!" width="1" height="660"></td>
        </tr>
        <tr> 
          <td height="26"></td>
          <td></td>
          <td></td>
        </tr>
        <tr> 
          <td height="160" colspan="4" valign="top"><img src="images/BottomLink.jpg" alt="A Golf Ball!" width="128" height="160"></td>
        </tr>
        <tr> 
          <td height="187">&nbsp;</td>
          <td>&nbsp;</td>
          <td></td>
          <td>&nbsp;</td>
        </tr>
        <tr> 
          <td height="23" colspan="7" valign="top"><img src="images/Bottom.gif" alt="Footer!" width="723" height="23"></td>
        </tr>
      </table>
    </div>
    </body>
    </html> 
    Is what i get for index.php?page=testfw
    You basicly have 2 HTML files in 1 PHP file - which is going to be killing the browser aswell due to the multiple <head> and <body> tags.
    Quote Originally Posted by Saracen View Post
    And by trying to force me to like small pants, they've alienated me.

  16. #16
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts
    surely i just want to check if teh address contains '?page=' and if it DOES then not do anything and if it doesn't, to execute the script?

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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