The Get variables of the URL need to be parsed. I have made a jQuery object of document.location and then have used the attr function to get the search attribute to get all the variables. But when i use the split function on it and after that each() is used it gives error stating that the object does not have a method each .
TypeError: Object [object Array] has no method 'each'
Code is :
$(document.location).attr('search').split('&').each()
I have also tried to use the search property in the first function but it does not allow it i.e $(document.location.search) gives error.
I have also checked that data type of the what is returned by split function, the console says that it is an object, i am also confused that it should have been an array.
P.S : all the above code goes in document.ready function of jQuery
Conclusion # The "split is not a function" error occurs when we call the split() method on a value that is not of type string. To solve the error, convert the value to a string by using the toString() method before you call split() , or make sure to only call the split method on strings.
It is possible to run jQuery and JavaScript together in you application. All you need to know is, which is a JavaScript Object when trying to run some methods of it. jQuery can be handy sometimes.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string.
The split() method splits a String object into an array of string by separating the string into sub strings. The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first. The join() method joins all elements of an array into a string.
Making a jQuery object from the document.location
object is pointless, because it's not a DOM element.
Just get the search
property from the object, and use the $.each
method intead of .each
as you are looping an array, not elements:
$.each(document.location.search.split('&'), function(){
...
});
Try this:
$.each($(document.location).attr('search').split('&'), function (index, value) {
alert(index + ': ' + value);
});
jQuery .each()
method is used to iterate over a jQuery object, executing a function for each matched element.
But what you get from the $(document.location).attr('search').split('&')
is a JavaScript array, which obviously has no method 'each': that is why you are getting the error.
To loop through an array in jQuery you need to use $.each()
method like above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With