Web Analytics Made Easy -
StatCounter URI - href - using & - CodingForum

Announcement

Collapse
No announcement yet.

URI - href - using &

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • URI - href - using &

    There is probably a very simple solution to this, but after weeping aloud for several hours I am unable to think straight.

    Problem Script: (added a space so that it displays properly)
    Code:
    <script type="text/javascript">
    function delItem(iC) {
    location.href = "myUri.asp?var1=5& amp;var2=6& amp;var3=" + iC;
    }
    </script>
    As you can see I have replaced the '&' with & amp;

    This works fine in Explorer / Opera, but in Netscape / Mozilla the & amp; is being displayed as & amp; in the address bar.

    One more thing, if I was to have:

    Code:
    <a href="myUri.asp?var1=5& amp;var2=6& amp;var3=6">here</a>
    This works fine in both Explorer and Gecko based browsers. Can any body tell me the difference and why the first script dosnt work in Gecko browsers?

    Thanks to anybody in advance
    Last edited by Out of Orbit; Mar 1, 2004, 03:02 PM.
    If it works first time it must be wrong.

  • #2
    Do you need the &amp;amp; in the script? Try just using just the &amp;

    Code:
    <script type="text/javascript">
    function delItem(iC) {
    location.href = "myUri.asp?var1=5&amp;var2=6&amp;var3=" + iC;
    }
    </script>
    Nobody is Perfect. I am Nobody.

    Comment


    • #3
      To make the page valid xhtml I do

      (according the w3c validation rules)
      If it works first time it must be wrong.

      Comment


      • #4
        The answer is to wrap the script in a CDATA block.
        Code:
        <script type="text/javascript">
        /* <![CDATA[ */
        
        function delItem(iC) {
          location.href = "myUri.asp?var1=5&var2=6&var3=" + iC;
        }
        
        /* ]]> */
        </script>
        liorean <[[email protected]]>
        Articles: RegEx evolt wsabstract , Named Arguments
        Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
        Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

        Comment


        • #5
          I was considering this before and it was going to be a kind of last resort thing. (or linking an external .js file which I think the validator ignours)

          I waste alot of time searching for solutions, but most of my time is wasted searching for why certain things happen.

          That is the case here, I dont understand why it works when coded directly into the html (<a href>) but not when accessed from a javascript function (well, atleast not in Gecko powered browsers).

          If I find out why this is happening I will let you know. (thats assuming you care).

          And thanks for your responces,
          If it works first time it must be wrong.

          Comment


          • #6
            This happens because when the script is inline within the HTML it's subject to the rules for HTML, one of which is that &amp; is required to be represented in the page as &amp;amp;.

            If you wrap it in the CDATA declaration as suggested or move it into an external file then it's no longer subject to the HTML rules which give you a lot more freedom.

            Does that explain the "why" for you?
            Check out the Forum Search. It's the short path to getting great results from this forum.

            Comment


            • #7
              If you wrap it in the CDATA declaration as suggested or move it into an external file then it's no longer subject to the HTML rules which give you a lot more freedom.

              Does that explain the "why" for you?
              Surley when its in the <script> tag it is still subject to the HTML rules? thats why its not valid xhtml when parsed??

              Thank you for your post anyway, it has been noted.
              If it works first time it must be wrong.

              Comment


              • #8
                Sorry, my last post just reiterated what you said.

                I am tired now
                If it works first time it must be wrong.

                Comment


                • #9
                  Just one more thing.

                  How come explorer unescapes it in the address bar but Gecko dosn't. (when within the <script> tag?)
                  If it works first time it must be wrong.

                  Comment


                  • #10
                    IE is buggy.
                    Check out the Forum Search. It's the short path to getting great results from this forum.

                    Comment

                    Working...
                    X