Jump to content

How do I replace a string to a search query only once?


nasekt

Recommended Posts

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 questionThe problem is, when I keep issuing the request it keeps appending over and over again, http://i.imgur.com/nKCvgKn.png

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);    }}

Basically, how do I tell javascript to NOT replace when search?q=+test1+test2+test3 is already present in the URL?

Can you please give me an example? I'm still new to programming.Thank you

Link to comment
Share on other sites

indexOf is a great way to check the value by examining the index position I left a few alert comments you can activate to track the activity.

 

came across this idea at http://stackoverflow.com/questions/7273312/what-is-the-best-way-to-check-if-a-string-exists-in-another

//geturlvar url=document.location.href;var stringCheck = "+test1+test2+test3";//alert(url);//check value of comparisonString.prototype.contains = function(substr) {  //alert(substr);    return this.indexOf(substr) > -1;}if(url.contains(stringCheck) == true){    //alert('NOT replace');    //code to do nothing}else {//alert('replace');//code to replace string}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...