CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Javascript/Jscript.NET :How do I replace a string to a search query only once?

  1. #1
    Join Date
    Sep 2014
    Posts
    6

    Javascript/Jscript.NET :How do I replace a string to a search query only once?

    My Goal: I want to programmatically append a string to Google Search.

    The code is JScript .NET, which is basically a .NET version of Javascript.Regardless of language, Anyone with appending type of skill can answer my question

    The problem is, when I keep issuing the request it keeps appending over and over again, http://i.imgur.com/nKCvgKn.png

    Code:
    if (oSession.uriContains("q=")) 
    {
        var str = oSession.fullUrl;
        var sAppend = "+test1+test2+test3";
        if (!oSession.uriContains(sAppend))
        {
            oSession.fullUrl = str.replace( "q=","q="+sAppend);
        }
    }
    Can you please give me an example? I'm still new to programming.

    Thank you

  2. #2
    Join Date
    Jun 2009
    Posts
    113

    Re: Javascript/Jscript.NET :How do I replace a string to a search query only once?

    If you work through what your code is doing, it does make sense. Say the first time the URI is
    Code:
    http://foo/search?q=bar
    you're then replacing the "?q=" with the "?q=+test1+test2+test3" so it now becomes:
    Code:
    http://foo/search?q=+test1+test2+test3bar
    and the next time round:
    Code:
    http://foo/search?q=+test1+test2+test3+test1+test2+test3bar
    What you want to do is keep everything up to (and possibly including) the "?q=" and append some new code. So you need to find the first part, and append the next, so in pure JavaScript terms, you want something like this:
    Code:
    var str = oSession.fullUrl;
    var sAppend = "test1+test2+test3";
    var sQuery = str.indexOf("?q=");
    if ( sQuery !== -1) {
    oSession.fullUrl = str.substr(0,sQuery+2) + sAppend;
    }
    Although there's probably some .Net code that will carve up the query string for just the bits you want.

  3. #3
    Join Date
    Sep 2014
    Posts
    6

    Re: Javascript/Jscript.NET :How do I replace a string to a search query only once?

    @the_cat

    First of all, thank you for replying.

    now, There are few problems with substr()

    1. when sQuery is set to +2, the page does not even render.
    2.So, I set it to higher value like +9, and typed codeguru in google, it gives me this :http://imgur.com/jyOzGmk (It cuts off when the string length is greater than "9", also it does not append if string length is less than "9")

    Any ideas?

    Thank you.

  4. #4
    Join Date
    Jun 2009
    Posts
    113

    Re: Javascript/Jscript.NET :How do I replace a string to a search query only once?

    Okay, then you need to find the location of the start of the query string (so the question mark) and strip out everything else to the right and replace it with the new query string as it's not finding "?q=" as there is a "?site=" instead:
    Code:
    var str = oSession.fullUrl;
    var sAppend = "?q=test1+test2+test3";
    var sQuery = str.indexOf("?");
    if ( sQuery !== -1) {
    oSession.fullUrl = str.substr(0,sQuery) + sAppend;
    }
    else {
    oSession.fullUrl = str + sAppend;
    }
    should cater for no query strings too.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)