0

I am working with the below Javascript function. It only works when the value is a number. I mean it only returns the value if it is a a number. For example:

var ldInstID = getParameterByName("ID")

If ID is a number then it works and assigns the value to the variable but if the ID is a string it is not working. Please help to make this work for a string too. I am using this on SharePoint list edit page where ID is a list column value. I want to capture another column city and pass it as href query string along with ID. In the attached images you can see that ldInstID is blank

<!--
    Name: dispParent.js
-->

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>


<script type="text/javascript">

jQuery(document).ready(function($) {

    //get the ID for the Issue from the Query String
   // var issueID = getParameterByName("ID");
 var ldInstID = getParameterByName("LeadInsitution");
    //find the element with the "Add new item" link.
    //note that if you have more than one list on your page, this just finds the first one
    var anchorElement = $("a[title='Add a new item to this list or library.']");
    
    //modify the "Add new item" link to call the "NewItem2" function and pass in the Issue ID. 
   //Be sure to put the path to your site below. You can use relative URL to the web application or the FQDN
  //  $(anchorElement).attr("href","javascript:NewItem2(event,'URL/Lists/Time/NewForm.aspx?IssueID="  + issueID +  "');");
   // $(anchorElement).attr("href","javascript:NewItem2(event,'URL/NewForm.aspx?IssueID="  + issueID + "&LdInst" + LdInst + "');");
   $(anchorElement).attr("href","javascript:NewItem2(event,'URL/NewForm.aspx?LdInstID="  + ldInstID +  "');");
    //remove the "onclick" attribute from the anchor element as we aren't using it anymore
    $(anchorElement).removeAttr("onclick");

});


// no, I didn't write this function from scratch, I found it at
// http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
// http://www.sharepointhillbilly.com/Lists/Posts/Post.aspx?ID=26
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

</script>

variable passed-ID Variable passed- ldInstID

CapLearn
  • 1
  • 1
  • 1
    The regex doesn't do anything with regards to digits/alpha characters. Are you certain the type of the value is the issue? Or is it the formatting of the querystring? Or is the problem actually how you use `ldIntID` *after* you retrieve it? What exactly is the error? – Rory McCrossan Nov 05 '19 at 17:30
  • Also, I'd suggest using this logic for your querystring parser: https://stackoverflow.com/a/901144/519413. I've used it before and it seems more robust than what you have – Rory McCrossan Nov 05 '19 at 17:31
  • Your value will by default be a string, unless you convert it. `var ldInstID = Number(getParameterByName("ID"));` should work for you (just be aware that it will be NaN if there is no ID parameter, or if it doesn't have a numerical value. – scunliffe Nov 05 '19 at 17:33
  • @scunliffe note that the OP states this *works* when the value is a number (using current logic) but *not* when it's a string. Exactly what that means is hard to say given the lack of useful context about the values we're dealing with – Rory McCrossan Nov 05 '19 at 17:34
  • Hi @user12327244 we need to know what you do with your `ldInstID` variable after you set it? I'm pretty sure you try to do some math with it, or compare it as a number... and that is why it fails. You may need to check if the value you get back is a number or not... and then **only** run your following code **if** it is a number (e.g. a valid value was passed in) – scunliffe Nov 05 '19 at 17:38
  • I have updated the post with my script where I am trying to capture Lead Institution name instead of ID and it does not work for that. it works fine for ID. I am new to this, please bear with me. My end goal is to be able to pass both ID and Lead Institution name. – CapLearn Nov 05 '19 at 17:44

1 Answers1

0

Get parameter by id from URL:

function getURLParameter(parameterName) {
    let result = null, temp = [];
    location.search
        .substr(1)
        .split('&')
        .forEach(function (item) {
            temp = item.split('=');
            if (temp[0] === parameterName)
                result = decodeURIComponent(temp[1]);
        });

    return result;
}

if my url is http://example.com?id1=100&text=my%20text

console.log(getURLParameter('id1')); // 100
console.log(getURLParameter('text')); // "my text"
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
  • Hi@scunliffe, All I want to do it grab the value and pass it on using href query string. The script works as long as the value assigned to the variable is a number but if I assigning text it is not working – CapLearn Nov 05 '19 at 21:11