SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I control the url shown at the navigation bar?

    Ok ... how do I actually put some javascript to control (a.k.a hard code) what is being shown on the navigation bar? (the place which shows the URL)

    E.g when ppl navigate to this url of mine-->

    http://www.mydomain.com/testingscripts.htm, i want them to see "http://www.google.com" on the navigation bar for example.

    How do I do that? TIA!
    Go increase web site traffic by using 27 proven strategies!

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,423
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    You can't do that for obvious security reasons.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    E.g when ppl navigate to this url of mine-->
    http://www.mydomain.com/testingscripts.htm, i want them to see "http://www.google.com" on the navigation bar for example.
    What a revolting thought- but at least it is not one you'll accomplish with javascript.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The other way around is possible: if people go to http://www.google.com and you own that domain + server(s) it's hosted on, you can use mod_rewrite (not a Javascript thing, btw) to have the server see http://www.google.com as http://www.mydomain.com/testingscripts.htm.

  5. #5
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok ... before you guys start getting the wrong idea.

    let's say that i want to append an extra string such as "?search" to the end of whatever the url is using javascript ... how do I achieve that?
    Go increase web site traffic by using 27 proven strategies!

  6. #6
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In order words, at the <head> section, I want to capture the current page's url and append a certain string to it. any ideas?
    Go increase web site traffic by using 27 proven strategies!

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't. The URL displays the current location. The only way to change the URL, is to change the location. If that's indeed what you want, you can do so with window.location.href = window.location.href + "?search". It's generally a better idea to use a link for these things though.

    There is one exception. You can append a string after a hash-mark, without changing location. Eg. window.location.href = window.location.href + "#search"

  8. #8
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks kyberfabrikken.

    Erm ... so how do I put this segment window.location.href = window.location.href + "?search" into my <head> section then?
    Go increase web site traffic by using 27 proven strategies!

  9. #9
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right now i'm using:

    <script language="javascript">
    window.location.href = window.location.href + "<?php $keyword ?>";
    </script> within the <head> section but obviously this wouldn't do as it would simply reload infinitely.

    How do I go about this?
    Go increase web site traffic by using 27 proven strategies!

  10. #10
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Caesar View Post
    How do I go about this?
    You can't get rid of the redirect, but you could limit it to happen once, which may be what you're trying to, if I understand you correctly. Instead of doing the redirect in javascript though, I'd strongly recommend that you do it from PHP (Or any other server side technology, but I see you use PHP). Assuming you want the keyword foobar to be appended to the URL, you could do:
    PHP Code:
    <?php
    // test if the requested URL ends with ?foobar
    if (!preg_match('\?foobar$'$_SERVER['REQUEST_URI'])) {
      
    // it didn't -- we'll redirect then
      
    header("Location: " $_SERVER['REQUEST_URI'] . "?foobar");
      
    // remember to halt execution after sending a Location header, or it won't work as expected.
      
    exit;
    }
    ?>
    <html>
    ... the rest of your page follows ...
    How it works is, that when the user first requests the page, the requested URL (REQUEST_URI) won't contain the keyword. The PHP script will detect this and redirect to the same URL, but with the keyword added on. The browser will then (automatically) request the new URL. The PHP script is executed again, but this time it doesn't redirect, because the URL contains the keyword. Finally, the page is displayed, and the URL has the keyword in it.

  11. #11
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken, thanks for your help.

    Ok, i've added that bit of code and it says:

    Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /home/myusername/public_html/mydomain/index.php on line 3

    Warning: Cannot modify header information - headers already sent by (output started at /home/myusername/public_html/mydomain/index.php:3) in /home/myusername/public_html/mydomain/index.php on line 5

    Line 3: if ....
    Line 5: header(..)

    Having said that, how do I use preg_match to match for any sort of string after the initial slash? (e.g mydomain.com/kvwj or even mydomain.com/phpasp)
    Go increase web site traffic by using 27 proven strategies!

  12. #12
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I forgot the delimiters. Try:
    PHP Code:
    if (!preg_match('~\?foobar$~'$_SERVER['REQUEST_URI'])) { 
    As for your second question, I think this will do:
    PHP Code:
    if (!preg_match('~^(http|https)://[^/]+/[^?]+~'$_SERVER['REQUEST_URI'])) { 
    You may find parse_url more handy, if you're feeling uncomfortable with regular expressions.


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
  •