String question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arjen

    String question

    Hello,

    I have inside a string a complete url.


    At the end you see "id=", the id can be "1", "23", "234", etc..

    Now I want to have the Id in a new variable.
    How can catch the id?

    Thanks!



  • Ken Smith

    #2
    Re: String question

    Possible solution:

    Copy and save as testme.html

    <HTML>

    <SCRIPT>

    //Get the full URL

    theFullURL=docu ment.location.h ref;

    document.write( "The Full URL:"+theFullUR L+"<BR>");

    //Index the ID part of string

    id=theFullURL.i ndexOf("id=");

    document.write( "Index of URL:<B>"+id+"</B><BR>");

    //Get the following string from index

    //(+2 to get rid of 'id=')

    idVal=theFullUR L.substring(id+ 2);

    document.write( "ID:<B>"+idVal+ "</B>");

    </SCRIPT>



    <HTML>

    When you run the HTML in explorer add your other values.

    (i.e

    ORIGINAL:

    C:\test\testme. html

    RUN Like:

    C:\test\testme. html?action=sho wnesitem&id=125

    )

    Worked for me!

    Ken

    "Arjen" <boah123@hotmai l.com> wrote in message
    news:c6bbj0$ti6 $1@news2.tilbu1 .nb.home.nl...
    [color=blue]
    > Hello,
    >
    > I have inside a string a complete url.
    > http://www.somedomain.com/index.php?...ewsitem&id=125
    >
    > At the end you see "id=", the id can be "1", "23", "234", etc..
    >
    > Now I want to have the Id in a new variable.
    > How can catch the id?
    >
    > Thanks!
    >
    >
    >[/color]


    Comment

    • Grant Wagner

      #3
      Re: String question

      Arjen wrote:
      [color=blue]
      > Hello,
      >
      > I have inside a string a complete url.
      > http://www.somedomain.com/index.php?...ewsitem&id=125
      >
      > At the end you see "id=", the id can be "1", "23", "234", etc..
      >
      > Now I want to have the Id in a new variable.
      > How can catch the id?
      >
      > Thanks![/color]

      If it's always "id=", just use:

      (new RegExp("id=(\\d +)")).test(wind ow.location.sea rch);
      var id = RegExp.$1;
      alert(id);

      window.location .search might look something like:
      "?param=blah&id =123&somethinge lse=something" so:

      (new
      RegExp("id=(\\d +)")).test("?pa ram=blah&id=123 &somethingelse= something");

      var id = RegExp.$1;
      alert(id); // alerts "123" which is correct

      The nice thing about using (new RegExp()).test( ); var x =
      RegExp.$1; is that you always either get the string you are looking
      for, or you get an empty string. It never causes an error, it never
      results in a null value. The *only* thing to be aware of is if you
      were looking for something other then numbers, then you need to
      make sure you don't pick up the entire query string:

      (new RegExp("param=( .[^&]+)")).test(wind ow.location.sea rch);

      Lastly, if you're looking for a lot of values on the query string,
      this mechanism is better replaced with something that retrieves all
      the key/value pairs as an object's properties/values. And this
      mechanism doesn't support multiple same named items on the query
      string (although it could be modified to).

      --
      | Grant Wagner <gwagner@agrico reunited.com>

      * Client-side Javascript and Netscape 4 DOM Reference available at:

      *


      * Internet Explorer DOM Reference available at:
      *
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


      * Netscape 6/7 DOM Reference available at:
      * http://www.mozilla.org/docs/dom/domref/
      * Tips for upgrading JavaScript for Netscape 7 / Mozilla
      * http://www.mozilla.org/docs/web-deve...upgrade_2.html


      Comment

      • Arjen

        #4
        Thanks

        Hello,

        Thanks for the support, it works fine!

        Arjen




        "Arjen" <boah123@hotmai l.com> schreef in bericht
        news:c6bbj0$ti6 $1@news2.tilbu1 .nb.home.nl...[color=blue]
        > Hello,
        >
        > I have inside a string a complete url.
        > http://www.somedomain.com/index.php?...ewsitem&id=125
        >
        > At the end you see "id=", the id can be "1", "23", "234", etc..
        >
        > Now I want to have the Id in a new variable.
        > How can catch the id?
        >
        > Thanks!
        >
        >
        >[/color]


        Comment

        Working...