none
SP intranet url value substring urlValue.indexOf Issue

    Question

  • I have a url during runtime

    urlValue =

    http://intranetportal.com/sites/PeopleSearch/Pages/peopleresultsPrint.aspx#Default={"k":"","r":[{"n":"Office","t":["\"ǂǂ426f73746f6e\"","\"ǂǂ4368696361676f\"","\"ǂǂ4443\"","\"ǂǂ4c6f7320416e67656c6573\"","\"ǂǂ4e657720596f726b\"","\"ǂǂ53616e204672616e636973636f\"","\"ǂǂ53696c69636f6e2056616c6c6579\"","\"ǂǂ554b\""],"o":"OR","k":false,"m":{"\"ǂǂ426f73746f6e\"":"Boston","\"ǂǂ4368696361676f\"":"Chicago","\"ǂǂ4443\"":"DC","\"ǂǂ4c6f7320416e67656c6573\"":"Los Angeles","\"ǂǂ4e657720596f726b\"":"New York","\"ǂǂ53616e204672616e636973636f\"":"San Francisco","\"ǂǂ53696c69636f6e2056616c6c6579 }}]}

    I tried substring test and returns correctly for single word such as Boston, Chicago. If I pass San Francisco, New York, it does not match and returns alert blank.

    Experts please help me to match two words such as "San Francisco" within the url string? Here is my script:

    var MasterOffice = "";

    var Office = ["Boston","Chicago","Los Angeles","New York","San Francisco"];

    for(i=0; i<Office.length; i++)
    {
       if ( urlValue.indexOf(Office[i]) != -1 )
       {

       MasterOffice += Office[i];
       }
    }

    alert(MasterOffice);

    Friday, February 9, 2018 1:34 AM

Answers

  • Hi,

    Please try to decode the url. Add the code below into a script editor web part in a web part page.

    <script type="text/javascript"> window.onload=function(){ var urlValue ='http://intranetportal.com/sites/PeopleSearch/Pages/peopleresultsPrint.aspx#Default={"k":"","r":[{"n":"Office","t":["\"ǂǂ426f73746f6e\"","\"ǂǂ4368696361676f\"","\"ǂǂ4443\"","\"ǂǂ4c6f7320416e67656c6573\"","\"ǂǂ4e657720596f726b\"","\"ǂǂ53616e204672616e636973636f\"","\"ǂǂ53696c69636f6e2056616c6c6579\"","\"ǂǂ554b\""],"o":"OR","k":false,"m":{"\"ǂǂ426f73746f6e\"":"Boston","\"ǂǂ4368696361676f\"":"Chicago","\"ǂǂ4443\"":"DC","\"ǂǂ4c6f7320416e67656c6573\"":"Los Angeles","\"ǂǂ4e657720596f726b\"":"New York","\"ǂǂ53616e204672616e636973636f\"":"San Francisco","\"ǂǂ53696c69636f6e2056616c6c6579 }}]}'; urlValue=decodeURIComponent(urlValue);

    var MasterOffice = ""; var Office = ["Boston","Chicago","Los Angeles","New York","San Francisco"]; for(i=0; i<Office.length; i++) { if ( urlValue.indexOf(Office[i]) != -1 ) { MasterOffice += Office[i]; } } alert(MasterOffice); } </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


    Friday, February 9, 2018 7:24 AM
    Moderator
  • It looks like some issue with SP page. I was able to get this achieved using javascript RegExp.

    var string = "test",
        expr
    = /es/; 
    expr
    .test(string);

    • Marked as answer by ShriG Friday, February 9, 2018 11:33 PM
    Friday, February 9, 2018 11:33 PM

All replies

  • Hi,

    Please try to decode the url. Add the code below into a script editor web part in a web part page.

    <script type="text/javascript"> window.onload=function(){ var urlValue ='http://intranetportal.com/sites/PeopleSearch/Pages/peopleresultsPrint.aspx#Default={"k":"","r":[{"n":"Office","t":["\"ǂǂ426f73746f6e\"","\"ǂǂ4368696361676f\"","\"ǂǂ4443\"","\"ǂǂ4c6f7320416e67656c6573\"","\"ǂǂ4e657720596f726b\"","\"ǂǂ53616e204672616e636973636f\"","\"ǂǂ53696c69636f6e2056616c6c6579\"","\"ǂǂ554b\""],"o":"OR","k":false,"m":{"\"ǂǂ426f73746f6e\"":"Boston","\"ǂǂ4368696361676f\"":"Chicago","\"ǂǂ4443\"":"DC","\"ǂǂ4c6f7320416e67656c6573\"":"Los Angeles","\"ǂǂ4e657720596f726b\"":"New York","\"ǂǂ53616e204672616e636973636f\"":"San Francisco","\"ǂǂ53696c69636f6e2056616c6c6579 }}]}'; urlValue=decodeURIComponent(urlValue);

    var MasterOffice = ""; var Office = ["Boston","Chicago","Los Angeles","New York","San Francisco"]; for(i=0; i<Office.length; i++) { if ( urlValue.indexOf(Office[i]) != -1 ) { MasterOffice += Office[i]; } } alert(MasterOffice); } </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


    Friday, February 9, 2018 7:24 AM
    Moderator
  • Your script should work, I think.

    You can use web browser debug tool to debug it.


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Friday, February 9, 2018 7:26 AM
  • Thanks Dennis.

    The sample urlValue I posted is from DecodeURIComponent. I cant trim the value of urlValue in a single line as its building dynamically and I am just reading the page url and assign to a urlValue.

    For some reason, if I use word without space, works just fine as you get but for cities with space 'New York', San Francisco' it results null. Is there any trimming I should use? Attached is the screen again to show how it appears:

    Any suggestions or thoughts for fix?

    Shri

    Friday, February 9, 2018 5:59 PM
  • It looks like this is only happing in SharePoint search result page. If I extract all JavaScript code as html and its working fine and displays correctly.   Any workaround to run this script in SharePoint search result page?

    Thanks

    Shri

    Friday, February 9, 2018 8:26 PM
  • It looks like some issue with SP page. I was able to get this achieved using javascript RegExp.

    var string = "test",
        expr
    = /es/; 
    expr
    .test(string);

    • Marked as answer by ShriG Friday, February 9, 2018 11:33 PM
    Friday, February 9, 2018 11:33 PM
  • Hi,

    Glad to hear that you solve this issue, thanks for your sharing.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, February 13, 2018 1:44 AM
    Moderator