1

The problem with the below code is that it appends string at the very end of the URL

if (oSession.uriContains("search?q=")) 
    {
        var str = oSession.fullUrl;
        var sAppend = "+test1+test2+test3";
        if (!oSession.uriContains(sAppend))
        {
            oSession.fullUrl = str + sAppend;
        }
    }

So, How can I conditionally place +test1+test2+test3 right next to search?q= ?

Thank you

jarid wilders
  • 25
  • 1
  • 7

1 Answers1

0

What's the problem with replace function:

if (oSession.uriContains("search?q=")) 
    {
        var str = oSession.fullUrl;
        var sAppend = "+test1+test2+test3";
        if (!oSession.uriContains(sAppend))
        {
            oSession.fullUrl = str.replace( "search?q=","search?q="+sAppend);
        }
    }
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Sorry for the late reply, when I keep issuing the request it keeps appending over and over again, http://imgur.com/nKCvgKn. This only happens to google. – jarid wilders Sep 18 '14 at 10:52
  • @jaridwilders Is this if part of any kind of loop? You can also add one more condition that checks if the URL already contains `sAppend ` string – npocmaka Sep 18 '14 at 12:07
  • Can you please give me an example?, I'm very new to javascript. – jarid wilders Sep 18 '14 at 12:17
  • @jaridwilders - `if (oSession.uriContains("search?q=") && !oSession.uriContains(sAppend))` - but you must define `sAppend` outside of the if statement. – npocmaka Sep 18 '14 at 17:08
  • I was able to partially solve the problem, If you type from main google page, Google does not start with `search?q=`, it starts with `search?site=&source=hp&q=&oq=`. How do I solve that? – jarid wilders Sep 19 '14 at 02:49
  • @jaridwilders - as I understand you want to make this a google search query? – npocmaka Sep 19 '14 at 08:34
  • @jaridwilders - what if you search for `"&q="` and replace it with `"&q=+test1+test2+test3"` – npocmaka Sep 19 '14 at 09:00
  • `"&q=+test1+test2+test3"`Does not work. Anyway, can you please review this code `oSession.fullUrl = oSession.fullUrl.Substring( 0,oSession.fullUrl.IndexOf("search?q=")) + sAppend ;` – jarid wilders Sep 19 '14 at 09:34
  • Wait!!.... I take that back, &q=+test1+test2+test3 does work,but when I click Next page in google. It browser freezes completetly. Any ideas? – jarid wilders Sep 19 '14 at 12:00
  • what browser you use? try with the debuging tools...Chrome and Firefox have the best toolsets for developers – npocmaka Sep 19 '14 at 12:05
  • 1
    Google instant search was the one freezing the broswer, solved the problem by disabling it in the search settings. Thank you for the help. – jarid wilders Sep 28 '14 at 15:38