Choose code to run based on incoming URL

Discussion in 'Javascript' started by Howard Kaikow, Oct 7, 2004.

  1. Can I use a bookmark to selectively choose the code to be run?



    For example. Suppose we have the following in an HTML file:



    <html>

    <head>

    <script language="JavaScript"><!--

    location.href = 'HKlinks.html#B'

    //--></script>

    <noscript>

    <meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">

    </noscript>

    </head>

    </html>



    And hklinks.html has:



    <html>

    <head>

    <a name="A"><script language="JavaScript"><!--

    location.href = 'HowardKaikow.html'

    //--></script></a>

    <a name="B"><script language="JavaScript"><!--

    location.href = HowardKaikowServices.html'

    //--></script></a>

    <noscript>

    <meta http-equiv="Refresh" content="3;url=index.html">

    </noscript>

    </head>

    </html>



    I want to choose which javascript to run based on the incoming link.

    I tried onload.target to choose, but I think I screwed up the sintax.


    --
    http://www.standards.com/; See Howard Kaikow's web site.
     
    Howard Kaikow, Oct 7, 2004
    #1
    1. Advertising

  2. Howard Kaikow

    McKirahan Guest

    "Howard Kaikow" <> wrote in message
    news:ck38sp$57t$...
    > Can I use a bookmark to selectively choose the code to be run?
    >
    >
    >
    > For example. Suppose we have the following in an HTML file:
    >
    >
    >
    > <html>
    >
    > <head>
    >
    > <script language="JavaScript"><!--
    >
    > location.href = 'HKlinks.html#B'
    >
    > //--></script>
    >
    > <noscript>
    >
    > <meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">
    >
    > </noscript>
    >
    > </head>
    >
    > </html>
    >
    >
    >
    > And hklinks.html has:
    >
    >
    >
    > <html>
    >
    > <head>
    >
    > <a name="A"><script language="JavaScript"><!--
    >
    > location.href = 'HowardKaikow.html'
    >
    > //--></script></a>
    >
    > <a name="B"><script language="JavaScript"><!--
    >
    > location.href = HowardKaikowServices.html'
    >
    > //--></script></a>
    >
    > <noscript>
    >
    > <meta http-equiv="Refresh" content="3;url=index.html">
    >
    > </noscript>
    >
    > </head>
    >
    > </html>
    >
    >
    >
    > I want to choose which javascript to run based on the incoming link.
    >
    > I tried onload.target to choose, but I think I screwed up the sintax.
    >
    >
    > --
    > http://www.standards.com/; See Howard Kaikow's web site.
    >
    >


    How about the following instead?

    <html>
    <head>
    <script type="text/javascript">
    location.href = "HKlinks.html?B";
    </script>
    <noscript>
    <meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">
    </noscript>
    </head>
    </html>


    <html>
    <head>
    <script type="text/javascript">
    var loc = location.search;
    if (loc == "?A") location.href = "HowardKaikow.html";
    if (loc == "?B") location.href = "HowardKaikowServices.html";
    </script>
    <noscript>
    <meta http-equiv="Refresh" content="3;url=index.html">
    </noscript>
    </head>
    </html>
     
    McKirahan, Oct 7, 2004
    #2
    1. Advertising

  3. On Thu, 07 Oct 2004 11:50:34 GMT, McKirahan <> wrote:

    [snip]

    >> I want to choose which javascript to run based on the incoming link.


    [snip]

    > How about the following instead?


    Since we're only talking about redirection, why not do it the proper way.
    With the server.

    [snip]

    Mike


    Please trim quotes!

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Oct 7, 2004
    #3
  4. "Michael Winter" <> wrote in message
    news:eek:psfh00z0bx13kvk@atlantis...
    > On Thu, 07 Oct 2004 11:50:34 GMT, McKirahan <> wrote:
    >
    > [snip]
    >
    > >> I want to choose which javascript to run based on the incoming link.

    >
    > [snip]
    >
    > > How about the following instead?

    >
    > Since we're only talking about redirection, why not do it the proper way.
    > With the server.


    Because my ISP limits what we can do and even then discourages server side
    stuff.

    I could use a custom 404 file, but I want to try javascript first.
    >
    > [snip]
    >
    > Mike
    >
    >
    > Please trim quotes!
    >
    > --
    > Michael Winter
    > Replace ".invalid" with ".uk" to reply by e-mail.
     
    Howard Kaikow, Oct 7, 2004
    #4
  5. Thanx, I'll give it a try.

    --
    http://www.standards.com/; See Howard Kaikow's web site.
    "McKirahan" <> wrote in message
    news:e4a9d.204833$D%.55378@attbi_s51...
    > "Howard Kaikow" <> wrote in message
    > news:ck38sp$57t$...
    > > Can I use a bookmark to selectively choose the code to be run?
    > >
    > >
    > >
    > > For example. Suppose we have the following in an HTML file:
    > >
    > >
    > >
    > > <html>
    > >
    > > <head>
    > >
    > > <script language="JavaScript"><!--
    > >
    > > location.href = 'HKlinks.html#B'
    > >
    > > //--></script>
    > >
    > > <noscript>
    > >
    > > <meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">
    > >
    > > </noscript>
    > >
    > > </head>
    > >
    > > </html>
    > >
    > >
    > >
    > > And hklinks.html has:
    > >
    > >
    > >
    > > <html>
    > >
    > > <head>
    > >
    > > <a name="A"><script language="JavaScript"><!--
    > >
    > > location.href = 'HowardKaikow.html'
    > >
    > > //--></script></a>
    > >
    > > <a name="B"><script language="JavaScript"><!--
    > >
    > > location.href = HowardKaikowServices.html'
    > >
    > > //--></script></a>
    > >
    > > <noscript>
    > >
    > > <meta http-equiv="Refresh" content="3;url=index.html">
    > >
    > > </noscript>
    > >
    > > </head>
    > >
    > > </html>
    > >
    > >
    > >
    > > I want to choose which javascript to run based on the incoming link.
    > >
    > > I tried onload.target to choose, but I think I screwed up the sintax.
    > >
    > >
    > > --
    > > http://www.standards.com/; See Howard Kaikow's web site.
    > >
    > >

    >
    > How about the following instead?
    >
    > <html>
    > <head>
    > <script type="text/javascript">
    > location.href = "HKlinks.html?B";
    > </script>
    > <noscript>
    > <meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">
    > </noscript>
    > </head>
    > </html>
    >
    >
    > <html>
    > <head>
    > <script type="text/javascript">
    > var loc = location.search;
    > if (loc == "?A") location.href = "HowardKaikow.html";
    > if (loc == "?B") location.href = "HowardKaikowServices.html";
    > </script>
    > <noscript>
    > <meta http-equiv="Refresh" content="3;url=index.html">
    > </noscript>
    > </head>
    > </html>
    >
    >
     
    Howard Kaikow, Oct 7, 2004
    #5
  6. How do I modify the code to handle both ? and # as bookmark delimiters?

    For example, in:
    <html>
    <head>
    <p><a href="HKlinks.html?B">B?</a></p>
    <p><a href="HKlinks.html?A">A?</a></p>
    <p><a href="HKlinks.html#B">B#</a></p>
    <p><a href="HKlinks.html#A">A#</a></p>
    </head>
    </html>

    Only the links using ? work.
    I commonly see # used instead f ? to separate the bookmark.


    What modifications are needed in the following HKlinks.html?
    <html>
    <head>
    <script type="text/javascript"><!--
    var loc=location.search;
    if(loc=="?A")location.href="HowardKaikow.html";
    if(loc=="?B")location.href="HowardKaikowServices.html";
    //--></script>
    <noscript>
    <meta http-equiv="Refresh" content="3;url=index.html">
    </noscript>
    </head>
    </html>
     
    Howard Kaikow, Oct 7, 2004
    #6
  7. Howard Kaikow

    McKirahan Guest

    "Howard Kaikow" <> wrote in message
    news:ck3dm4$8sb$...
    > How do I modify the code to handle both ? and # as bookmark delimiters?
    >
    > For example, in:
    > <html>
    > <head>
    > <p><a href="HKlinks.html?B">B?</a></p>
    > <p><a href="HKlinks.html?A">A?</a></p>
    > <p><a href="HKlinks.html#B">B#</a></p>
    > <p><a href="HKlinks.html#A">A#</a></p>
    > </head>
    > </html>
    >
    > Only the links using ? work.
    > I commonly see # used instead f ? to separate the bookmark.
    >
    >
    > What modifications are needed in the following HKlinks.html?
    > <html>
    > <head>
    > <script type="text/javascript"><!--
    > var loc=location.search;
    > if(loc=="?A")location.href="HowardKaikow.html";
    > if(loc=="?B")location.href="HowardKaikowServices.html";
    > //--></script>
    > <noscript>
    > <meta http-equiv="Refresh" content="3;url=index.html">
    > </noscript>
    > </head>
    > </html>
    >


    First, you should no longer use "<!--" and "--> within <script> tags.

    Second, your <a> tags go inside the <body> not the <head> section.

    Thirdly, you have a solution with "?" why try to use "#"?

    "#" are used for bookmarks (as you know).

    "?" indicates a QueryString which can be interrogated by the called page.
     
    McKirahan, Oct 7, 2004
    #7
  8. Because when I gove somebody a URL, I usually give it as a web page and,
    maybe, a bookmark on that web page, which is also how a lot of folkes are
    used to seeing a URL.

    I'll have no control whether they use a ? or a #.
    Here's what I have in the .js file, but the part about the # is doing what I
    need.

    <html>
    <head>
    <script type="text/javascript", SRC ="links.js"></script>
    <noscript>
    <HR>
    <A href="index.html">The URL you used requires the use of JavaScript. If you
    wish to use the URL,
    please enable support for Javascript in your browser.
    You will shortly be taken to http://www.standards.com/index.html, or you can
    click here now.</A>
    <HR>
    <meta http-equiv="Refresh" content="0;url=index.html">
    </noscript>
    </head>
    </html>

    The following is currently the link.js file.

    var loc=location.search.toUpperCase();
    if((loc=="?A")||(loc=="#A")){
    location.href="HowardKaikow.html";
    }
    else if((loc=="?B")||(loc=="#B")){
    location.href="HowardKaikowServices.html";
    }
    else{
    alert(location.href + " is not a valid URL.\n" +
    "You will be taken to http://www.standards.com/index.html.");
    location.href="index.html";
    }


    --
    http://www.standards.com/; See Howard Kaikow's web site.
    "McKirahan" <> wrote in message
    news:ZZa9d.197917$MQ5.6432@attbi_s52...
    > "Howard Kaikow" <> wrote in message
    > news:ck3dm4$8sb$...
    > > How do I modify the code to handle both ? and # as bookmark delimiters?
    > >
    > > For example, in:
    > > <html>
    > > <head>
    > > <p><a href="HKlinks.html?B">B?</a></p>
    > > <p><a href="HKlinks.html?A">A?</a></p>
    > > <p><a href="HKlinks.html#B">B#</a></p>
    > > <p><a href="HKlinks.html#A">A#</a></p>
    > > </head>
    > > </html>
    > >
    > > Only the links using ? work.
    > > I commonly see # used instead f ? to separate the bookmark.
    > >
    > >
    > > What modifications are needed in the following HKlinks.html?
    > > <html>
    > > <head>
    > > <script type="text/javascript"><!--
    > > var loc=location.search;
    > > if(loc=="?A")location.href="HowardKaikow.html";
    > > if(loc=="?B")location.href="HowardKaikowServices.html";
    > > //--></script>
    > > <noscript>
    > > <meta http-equiv="Refresh" content="3;url=index.html">
    > > </noscript>
    > > </head>
    > > </html>
    > >

    >
    > First, you should no longer use "<!--" and "--> within <script> tags.
    >
    > Second, your <a> tags go inside the <body> not the <head> section.
    >
    > Thirdly, you have a solution with "?" why try to use "#"?
    >
    > "#" are used for bookmarks (as you know).
    >
    > "?" indicates a QueryString which can be interrogated by the called page.
    >
    >
     
    Howard Kaikow, Oct 7, 2004
    #8
  9. "Howard Kaikow" <> wrote in message
    news:ck3qi2$svn$...
    > Here's what I have in the .js file, but the part about the # is doing what

    I
    > need.


    I meant to say "but the part about the # NOT is doing what I need".


    >
    > <html>
    > <head>
    > <script type="text/javascript", SRC ="links.js"></script>
    > <noscript>
    > <HR>
    > <A href="index.html">The URL you used requires the use of JavaScript. If

    you
    > wish to use the URL,
    > please enable support for Javascript in your browser.
    > You will shortly be taken to http://www.standards.com/index.html, or you

    can
    > click here now.</A>
    > <HR>
    > <meta http-equiv="Refresh" content="0;url=index.html">
    > </noscript>
    > </head>
    > </html>
    >
    > The following is currently the link.js file.
    >
    > var loc=location.search.toUpperCase();
    > if((loc=="?A")||(loc=="#A")){
    > location.href="HowardKaikow.html";
    > }
    > else if((loc=="?B")||(loc=="#B")){
    > location.href="HowardKaikowServices.html";
    > }
    > else{
    > alert(location.href + " is not a valid URL.\n" +
    > "You will be taken to http://www.standards.com/index.html.");
    > location.href="index.html";
    > }
    >
    >
    > --
    > http://www.standards.com/; See Howard Kaikow's web site.
    > "McKirahan" <> wrote in message
    > news:ZZa9d.197917$MQ5.6432@attbi_s52...
    > > "Howard Kaikow" <> wrote in message
    > > news:ck3dm4$8sb$...
    > > > How do I modify the code to handle both ? and # as bookmark

    delimiters?
    > > >
    > > > For example, in:
    > > > <html>
    > > > <head>
    > > > <p><a href="HKlinks.html?B">B?</a></p>
    > > > <p><a href="HKlinks.html?A">A?</a></p>
    > > > <p><a href="HKlinks.html#B">B#</a></p>
    > > > <p><a href="HKlinks.html#A">A#</a></p>
    > > > </head>
    > > > </html>
    > > >
    > > > Only the links using ? work.
    > > > I commonly see # used instead f ? to separate the bookmark.
    > > >
    > > >
    > > > What modifications are needed in the following HKlinks.html?
    > > > <html>
    > > > <head>
    > > > <script type="text/javascript"><!--
    > > > var loc=location.search;
    > > > if(loc=="?A")location.href="HowardKaikow.html";
    > > > if(loc=="?B")location.href="HowardKaikowServices.html";
    > > > //--></script>
    > > > <noscript>
    > > > <meta http-equiv="Refresh" content="3;url=index.html">
    > > > </noscript>
    > > > </head>
    > > > </html>
    > > >

    > >
    > > First, you should no longer use "<!--" and "--> within <script> tags.
    > >
    > > Second, your <a> tags go inside the <body> not the <head> section.
    > >
    > > Thirdly, you have a solution with "?" why try to use "#"?
    > >
    > > "#" are used for bookmarks (as you know).
    > >
    > > "?" indicates a QueryString which can be interrogated by the called

    page.
    > >
    > >

    >
    >
     
    Howard Kaikow, Oct 7, 2004
    #9
  10. Howard Kaikow

    McKirahan Guest

    "Howard Kaikow" <> wrote in message
    news:ck3qi2$svn$...
    > Because when I gove somebody a URL, I usually give it as a web page and,
    > maybe, a bookmark on that web page, which is also how a lot of folkes are
    > used to seeing a URL.
    >
    > I'll have no control whether they use a ? or a #.
    > Here's what I have in the .js file, but the part about the # is doing what

    I
    > need.
    >
    > <html>
    > <head>
    > <script type="text/javascript", SRC ="links.js"></script>
    > <noscript>
    > <HR>
    > <A href="index.html">The URL you used requires the use of JavaScript. If

    you
    > wish to use the URL,
    > please enable support for Javascript in your browser.
    > You will shortly be taken to http://www.standards.com/index.html, or you

    can
    > click here now.</A>
    > <HR>
    > <meta http-equiv="Refresh" content="0;url=index.html">
    > </noscript>
    > </head>
    > </html>
    >
    > The following is currently the link.js file.
    >
    > var loc=location.search.toUpperCase();
    > if((loc=="?A")||(loc=="#A")){
    > location.href="HowardKaikow.html";
    > }
    > else if((loc=="?B")||(loc=="#B")){
    > location.href="HowardKaikowServices.html";
    > }
    > else{
    > alert(location.href + " is not a valid URL.\n" +
    > "You will be taken to http://www.standards.com/index.html.");
    > location.href="index.html";
    > }


    First, you can change
    if((loc=="?A")||(loc=="#A")){
    to
    if(loc=="?A"||loc=="#A"){
    if you want.

    Second, what do you mean that you "... have no control whether they use a ?
    or a #" after you state "... when I gove [sic] somebody a URL". If you give
    them a URL it has what ever suffix you include; such as "?A".

    Thirdly, it still isn't clear what your really trying to accomplish (big
    picture).

    It isn't clear why you need to use "#"; is it that you want to jump "into" a
    page and not just "to" a page?
     
    McKirahan, Oct 7, 2004
    #10
  11. I figured out how to handle both ? and #.

    I also stumbled upon an easier way get help.
    By putting the code in a .js file, I can right click and open in VS .NET
    ..2003, which gives me the JScript help, etc.
     
    Howard Kaikow, Oct 8, 2004
    #11
  12. "McKirahan" <> wrote in message
    news:fai9d.218509$3l3.31200@attbi_s03...
    > Second, what do you mean that you "... have no control whether they use a

    ?
    > or a #" after you state "... when I gove [sic] somebody a URL". If you

    give
    > them a URL it has what ever suffix you include; such as "?A".


    Yes, but there's no reason to not also accept "#A".
    I figured out how to do it, see below.

    > Thirdly, it still isn't clear what your really trying to accomplish (big
    > picture).


    I have a very narrow scope in mind.

    I have a web site that has a number of pages I often reference in
    newsgroup/web forum postings.
    For example, a popular one is
    http://www.standards.com/OhMyWord/WordVBABooks.htm, another recent one has
    been
    http://www.standards.com/OhMyWord/VBABugsFeatures/FailureToDeleteToolbar.html.

    To make it easier for me to remember URLs to give to others and to
    facilitate changing the structure of the web site, I decided that a "links"
    page would be the right way to go. Server side links would be better, but I
    do not yet know how to do that. Should not be difficult to make a custom 404
    page, but I do not yet know how. I won't learn PHP just for this purpose.

    I would always give URLs to the links page and the link would then be
    redirected to the real page, so the above URLs would become, say:

    http://www.standards.com/links.html#WordVBABooks and
    http://www.standards.com/links.html#FailureToDeleteToolbar

    Those URLs would be much easier to remember.

    Of course, those URLs would only work if the user had Javascript enabled.
    Otherwise, I will redirect to http://www.standards.com/.

    The hard coded URL could still be used, but I won't guarantee those would
    not change.

    > It isn't clear why you need to use "#"; is it that you want to jump "into"

    a
    > page and not just "to" a page?


    All the URLs will be pointing to an anchor in the same page, each of which
    will be redirected to the real URL.
    If both ? and # work, then I see no reason why my script should not handle
    both.

    function CompareBookmarks(bmkTarget)
    { var temp = bmkTarget.toLowerCase();
    return (locSearch=="?" + temp)||(locHash=="#" + temp);
    }
    var locHash=location.hash.toLowerCase();
    var locSearch=location.search.toLowerCase();
    if(CompareBookmarks("HowardKaikow")){
    location.href="HowardKaikow.html";
    }
    else if(CompareBookmarks("HowardKaikowServices")){
    location.href="HowardKaikowServices.html";
    }
    else if(CompareBookmarks("SortPerformanceComparison")){
    location.href="Sorting/SortPerformanceComparison-Description.html";
    }
    else if(CompareBookmarks("ThisandThat")){
    location.href="index.html#ThisandThat";
    }
    else if(CompareBookmarks("CopyFileToPrinter")){
    location.href="ThisAndThat/CopyFileToPrinter.html";
    }
    else{
    alert(location.href + " is not a valid URL.\n" +
    "You will be taken to http://www.standards.com/index.html.");
    location.href="index.html";
    }
     
    Howard Kaikow, Oct 8, 2004
    #12
  13. Howard Kaikow

    McKirahan Guest

    "Howard Kaikow" <> wrote in message
    news:ck4o9k$sdl$...
    > I figured out how to handle both ? and #.
    >
    > I also stumbled upon an easier way get help.
    > By putting the code in a .js file, I can right click and open in VS .NET
    > .2003, which gives me the JScript help, etc.



    I'm glad that you figured it out...

    The reason "#" is handled differently than "?" is because "?" denotes a
    QueryString which is returned via location.search.
     
    McKirahan, Oct 8, 2004
    #13
  14. "McKirahan" <> wrote in message
    news:9Jm9d.142129$wV.114066@attbi_s54...
    > "Howard Kaikow" <> wrote in message
    > news:ck4o9k$sdl$...
    > > I figured out how to handle both ? and #.
    > >
    > > I also stumbled upon an easier way get help.
    > > By putting the code in a .js file, I can right click and open in VS .NET
    > > .2003, which gives me the JScript help, etc.

    >
    >
    > I'm glad that you figured it out...
    >
    > The reason "#" is handled differently than "?" is because "?" denotes a
    > QueryString which is returned via location.search.


    I saw that.

    But I also now see that ? and # are mot interchangeable.
    I found one page for which ? works, but # does not.


    http://www.standards.com/links.html?FailureToDeleteToolbar

    http://www.standards.com/links.html#FailureToDeleteToolbar
     
    Howard Kaikow, Oct 8, 2004
    #14
  15. I solved the back button problem by using the replace method of the location
    object.

    --
    http://www.standards.com/; See Howard Kaikow's web site.
    "Howard Kaikow" <> wrote in message
    news:ck38sp$57t$...
    > Can I use a bookmark to selectively choose the code to be run?
    >
    >
    >
    > For example. Suppose we have the following in an HTML file:
    >
    >
    >
    > <html>
    >
    > <head>
    >
    > <script language="JavaScript"><!--
    >
    > location.href = 'HKlinks.html#B'
    >
    > //--></script>
    >
    > <noscript>
    >
    > <meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">
    >
    > </noscript>
    >
    > </head>
    >
    > </html>
    >
    >
    >
    > And hklinks.html has:
    >
    >
    >
    > <html>
    >
    > <head>
    >
    > <a name="A"><script language="JavaScript"><!--
    >
    > location.href = 'HowardKaikow.html'
    >
    > //--></script></a>
    >
    > <a name="B"><script language="JavaScript"><!--
    >
    > location.href = HowardKaikowServices.html'
    >
    > //--></script></a>
    >
    > <noscript>
    >
    > <meta http-equiv="Refresh" content="3;url=index.html">
    >
    > </noscript>
    >
    > </head>
    >
    > </html>
    >
    >
    >
    > I want to choose which javascript to run based on the incoming link.
    >
    > I tried onload.target to choose, but I think I screwed up the sintax.
    >
    >
    > --
    > http://www.standards.com/; See Howard Kaikow's web site.
    >
    >
     
    Howard Kaikow, Oct 9, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Wilson
    Replies:
    6
    Views:
    374
    Sidharthk
    Nov 16, 2003
  2. Electric Co.
    Replies:
    7
    Views:
    643
    Electric Co.
    Dec 7, 2006
  3. Jens
    Replies:
    6
    Views:
    469
    Flash Gordon
    Jan 24, 2007
  4. FerrisUML

    Choose Membership Provider at Run-time

    FerrisUML, Apr 14, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    517
    Cowboy \(Gregory A. Beamer\)
    Apr 14, 2008
  5. michael75
    Replies:
    0
    Views:
    1,852
    michael75
    Aug 3, 2010
Loading...

Share This Page