IE11 not accepting string(variable) as parameter to localStorage.setItem() - javascript

Does anyone knows why IE does not accept a string-variable as a parameter to the method setItem?
Works fine on Chrome.
Example on IE:
This works:
var itemName = 'anyname';
localStorage.setItem(itemName, 'anything');
This doesn´t:
var itemName = 'anyname';
var stringName = 'some string content';
localStorage.setItem(itemName, stringName );
This gives 'Invalid argument error'.
Any help on that?
Thanks! :)
EDIT.
That post relates about another problem, the example given in that post(the one that does not work for him) actually works for me!
My problem shows that the method setItem does not accept a string variable but accepts a normal enclosed quoted string.
Also the solution given in the related post is not acceptable to my problem, I can´t expect that the end user will install a IE11 bug-fix.

After heavy search, the problem lie in the contents of your string. IE11 setItem method does not accept certain chars. My original string´s contents had things like '|' and '~'.
The only workaround I find is to use the encodeURI(yourStringHere) before sending it to the setItem method and after decodeURI it.

Related

Pass Component Name as Argument and then attach method (not working?)

Maybe I'm not using the right terms/names for my searches but I have not been able to find anything on this topic. I would have thought this would be an easy thing to find. What I'm doing is passing a component name to a function and then trying to use the component's name by attaching a method to it. I have confirmed (via Dev Tools) that the name is correctly being passed but when I use the variable and attach the method the specific request does not work. If I 'hard-code' the exact component name to the method it works perfectly. This makes me think the (various) ways I've been trying to attach the method to the variable name is incorrect (see various attempts below). Can you offer some direction here? Thank you.
Passing to Function ...
const grid_name = "grid_GroupA";
console.log(grid_name); // Shows grid_GroupA
msg_max(newItem, grid_name);
Function (only listing relevant parts)
function msg_max(newItem, grid_target) {
console.log(grid_target); // Shows grid_GroupA
// grid_GroupA.data.add(newItem); // This works ...
// grid_target.data.add(newItem); // This does not work
// (grid_target).data.add(newItem); // This does not work
// [grid_target].data.add(newItem); // This does not work
// grid_target + '.data.add(newItem)'; // This does not work
Thank you ...
Edit ...
In my attempt to provide detail I hope I haven't confused the issue.
In essence, my question is if I can type this exact string
grid_GroupA.data.add(newItem);
and it works for my function, how can I place a variable with the exact string "grid_GroupA" in front of ".data.add(newItem);" and have it seen the same as the line of code that works? Maybe my lack of knowledge here is getting in the way but isn't the line of code that works just a string that is then used to 'find' the object? So, if that assumption is correct, how do I create that same string with the variable? If my assumption is wrong I am a willing learner so I will be all ears. Thank you.
I do not see how grid_target is an object. You are passing grid_name(which is a string) to the function, so grid_target will have no data property, because string doesn't have such a member.
P.S. snake_case is bad option for JavaScript, consider using cameCase instead

Truncate a string after match a specific word in smarty or javascript/jQuery

I am working on a website but somehow I have sometimes little bug coming from the add of content after the end of some string.
For example :
style="background-image:url({$sub.image});" return
style="background-image:url(http://blablalba.fr/phoenix.jpg%3Fv%3D1);"
As you can see there is %3Fv%3D1 after the end on the link (after .jpg). I don't know where this is coming from and I'd like to erase it.
So my question is : in smarty is there a way to truncate a String after matching a word ?
And if it is not possible, what is the easiest way in Js/jQuery ?
I tough of this Regex to match the correct string : /^(.*?\.jpg)/
So it lead me to this :
$('.append').each('li',function(){
var newLink = $(this).find('div').css('background-image').match(/^(.*?\.jpg)/);
$(this).find('div').css('background-image',newLink);
})
But this is still buggy so if someone as a better idea of how shall I do this ?
Here is a JsFiddle if it can provide some help.
Edit + Answer
Ok so as has said #Hitmands just below it was a problem of URI encoding.
So this is the clearest and smartest solution :
$('.imgLittle').each(function() {
newLink = decodeURIComponent($(this).css('background-image'));
$(this).css('background-image',newLink)
})
And here is the final (working) JsFiddle.
Hope this could help someone once.
If you try to exec this window.decodeURIComponent("%3Fv%3D1") in your console it returns a simply "?v=1", this could be a way to manage the client-caching... when the v (version?) param changes it forces the browser to download the resource even is already cached...
I think that this is an important information that you cannot strip!
A smart solution could be a simply decoding of this param.... you should, but I'm not sure, try http://www.smarty.net/docs/en/language.modifier.unescape.tpl...
If isn't possible to decode the URI Component via smarty, you can do it via Javascript using the code that you posted below with the window.decodeURIComponent instead of regex-stripping.

passing veriable in url weird behaviour

I'm trying to pass a variable throught URL and it's doing something really weird in Chrome and Firefox, it seems is doing the same in OS.
The issue is that I'm trying to send a few variable in the URL this way:
document.location.href = 'http://www.ihaves.org/main.php?id='+facebookUser+'&first_name='+facebookName+'&last_name='+facebookName+'&locat='+locat_+'&locationlat='+facebookLat+'&locationlon='+facebookLon+'&mode=facebook';
The weird result is this:
http://www.ihaves.org/Vejer%20de%20la%20Frontera,%20Spain
when the expected result should be this one: (taken from IE)
http://www.ihaveseen.org/main.php?id=1215834998&first_name=Juanma%20De%20Los%20Santos&last_name=Juanma%20De%20Los%20Santos&locat=Vejer%20de%20la%20Frontera%20Spain&locationlat=36.25&locationlon=-5.96667&mode=facebook
I have detected that the problem is the variable called "locat", if I take it out then everything seems to go fantastic so I have thought that the comma of "Vejer de la Frontera, Spain" might be the problem so I have used:
var location_name = locationName.replace(/,/g, '');
But it keeps doing the same weird thing than before. Any idea? Does chrome accept an espace beetwen words? Remember firefox does exactly the same. By the way, I'm using GET_ in the PHP.
You should use encodeURI() on the Parameters when constructing the URL.
document.location.href = 'http://www.ihaves.org/main.php?id=' + encodeURIComponent(facebookUser);
Use:
encodeURIComponent(facebookName)
encodeURIComponent
You should be using encoded key/value pairs. Some characters will result in an invalid URL. For example:
mysite.com?name=Bed&Breakfast
will result in two values "name=Bed" and "Breakfast=" which is not what you would expect or want. Encoding would result in your URL:
mysite.com?name=Bed%26Breakfast
It may look funny but the URL is now valid and works as expected.

Why is 'indexOf' not returning anything?

The book I am reading tells me to open up the JavaScript console and try the code "foo: bar".indexOf(":"). I've tried it in many ways. I tried removing quotation marks, putting it inside a show() and alert() function. I just can't seem to tease anything out.
Has something changed in JavaScript? Has the author made a mistake? Am I supposed to get no return? Do I need to append document.write, perhaps? Any help greatly appreciated.
Yes something changed in Firefox 5+
However the console (ctrl-shift-k) still works
In the error console (ctrl-shift-J) you will need to wrap it in alert:
foo:bar is a property definition in json, and indexOf is supposed to deal with a left value (string variable, constant, or at least something that can have characters in it. I don't know why the book you are reading wants you to do this, but it doesn't seem to be correct. The correct way to use indexOf would be :
var myObject = {
foo:"bar"
}
alert(myObject.foo.indexOf("a"));
try like follows, it should work. Generally the indexOf() will return -1 if the value to search for never occurs.
var str="foo:bar";
document.write(str.indexOf(":") + "<br />");
The output should be 3

jQuery .val() works correctly - unless I pass it the vaue I really need

Here is a simple chunk of code:
1 var selected_clone = selected.clone();
2 alert(selected_clone.text());
3 var new_value = selected_clone.text();
4 form_li.find('input.ajax_selection').val(new_value);
Now.
Line 2 (debugging) outputs exactly the value I expect it to.
Line 3 is redundant, I know, but I used it for testing: when passing an arbitrary string to new_value, the val in line 4 works perfectly.
It does not change the value if I assign it the result of selected_clone.text()
The question is: why does it behave in such a puzzling way?
From chrome's debugging console, just chilling silence.
Additional info:
typeof(new_value) is string;
the value in the form field is actually updated: the value attribute, however is not.
about the latter point: no, it's not my debugger. the values sent on submit are non-updated.
the request sends the unupdated value; while the form displays the updated one.
Holy Shitzu this is weird.
I solved the problem by changing line 4 to: form_li.find('input.ajax_selection').attr('value',new_value);.
That does not make the slightest amount of sense to me though, and I would still like to know why.
The answer to your new question can be found in that question: .prop() vs .attr()
To understand you will need to know that val() is a shortcut to prop('value')
I would first try to know how many objects the form_li.find(...) method returns. Then I would check if
form_li.find('input.ajax_selection').attr("value",new_value)
works or not. Difficult to answer with so few code, sorry

Categories

Resources