substr in IE7 not working - javascript

I'm trying the following code to extract first few characters from a string stored in a variable.
title.substr(0,35)+"_"+var1+"_"+var2+var3;
At the above line IE7 throws an error:
Object doesn't support this property or method.
Tried: substring and slice as well. Still same issue.

It is quite apparent from the error message that title is not of type string.
Try this:
var title = "abcdefghijklmnopqrstuvwxyz";
console.log(title.substr(0,15));
I suspect your title variable is not of type String. To debug it, try
console.log(typeof(title)) in your own code.
Check the console and see what you get.

Related

JSON.parse() fails when a string contains the "null" substring... for example with italian words like "annullo" or "annullare"

Good morning everyone and thank you in advance for any suggestions. I have written a small web application to perform simple searches in a stamps database using php and javascript.
The server sends to the browser the whole database as a JSON and the queries are done client-side with a javascript code.
The JSON has this structure:
{"ck":0,"db":[["string11","string12","string13"],["string21","string22","string23"], etc... } .
Until now the system has worked perfectly and over 1500 stamps could be shown.
Suddenly it stopped working and, in the browser's Javascript console, this error message appeared:
VM672:1 Uncaught SyntaxError: Expected ',' or ']' after array element in JSON at position 97506 at JSON.parse (<anonymous>) ...etc...
After a series of tests, by exclusion I came to discover that it was the word "annullo" in the last added record to generate the error.
I guess it could be the substring "null" to give problems, but I have no idea how to escape it.
A really strange thing is that, whilst failing with the JSON.parse() function, browser's javascript console, as well as other json validation tools, recognise the server's response as a valid JSON.
Thanks for any help!
As #Andrea Soffiantini pointed in a comment, this was the actual problem:
Client side I had this command a few lines before parsing: data = data.replaceAll("null","\"\""); , where data is the json as a string, as received from server.
This replaces the string "anullo" with "a""o", which is invalid in JSON syntax.
I think I understand the problem. All strings are enclosed in quotes.
{example: "example"}
In the development of this case they must have put "annullo" but they forgot to insert the quotes. If it was null, not a string, it could look like this, without quotes.
json = {example: null}
However, as annullo is an invalid word in javascript and is not as a string, it gives an error.
The correct thing to do is to correct the error in the source, transforming it to null, however, I believe that you can also use this regular expression. But do tests to ensure that there is no undue substitution elsewhere.
json.replace(/.+["']: ?annull(o|are)[\n,}]$/g, matched => {
return matched.replace(/annull(o|are)/, 'null')
})

I must replace some special characters on Code by Zapier

I need to replace some special characters on Zapier.
Since there are many substitutions, I want to do it through Code by Zapier.
I made the code, but I got the following error on Code by Zapier:
We had trouble sending your test through.
TypeError: str.replace is not a function
Here is the code:
str = inputData;
str.replace(/Á|Ä|À|Ã|Â/g, "A");
str.replace(/É|Ë|È|Ê/g, "E");
str.replace(/Í|Ï|Ì|Î/g, "I");
str.replace(/Ó|Ö|Ò|Õ|Ô/g, "O");
str.replace(/Ú|Ü|Ù|Û/g, "U");
output = [{outputData: str}];
I don't know why str.replace doesn't work on Code by Zapier. I tried use str = str.replace(), instead str.replace(), but it did not worked too.
Can someone help?
Looks like inputData is not a string but rather an object. So if you provided a field to the Code then you need to access it as a property of that object. Let's say the name of the variable is foo, then you need to access it as inputData.foo.replace() (see screenshot).

Jquery: "Expected Comma but found <character>" error in Netbeans/Eclipse

Following is code which is giving above error, in netbeans as well as eclipse:
$(".bandListing").autocomplete({
source : [ ${bandnavn} ]
});
The error says:
1) Expected Comma but found {
source:[${bandnavn}]
2) Expected Comma but found ]
source:[${bandnavn}]
${bandnavn} is JSP object containing String of comma separated values which is put into array and assign to source. The code output is fine
Following is screenshot from Netbeans of same; could anyone guide me how to rectify the same?
If it could be fixed without do much changes, that will be helpful.
Store the value returned ${bandnavn} in a JavaScript variable, then use split() to create an array of string which can be supplied to autocomplete
var str = '${bandnavn}';
$(".bandListing").autocomplete({
source : str.split(',')
});

Javascript regex match fails on actual page, but regex tests work just fine

I have a very specific problem concerning a regular expression matching in Javascript. I'm trying to match a piece of source code, more specifically a portion here:
<TD WIDTH=100% ALIGN=right>World Boards | Olympa - Trade | <b>Bump when Yasir...</b></TD>
The part I'm trying to match is boardid=106121">Olympa - Trade</a>, the part I actually need is "Olympa". So I use the following line of JS code to get a match and have "Olympa" returned:
var world = document.documentElement.innerHTML.match('/boardid=[0-9]+">([A-Z][a-z]+)( - Trade){0,1}<\/a>/i')[1];
the ( - Trade) part is optional in my problem, hence the {0,1} in the regex.
There's also no easier way to narrow down the code by e.g. getElementsByTagName, so searching the complete source code is my only option.
Now here's the funny thing. I have used two online regex matchers (of which one was for JS-regex specifically) to test my regex against the complete source code. Both times, it had a match and returned "Olympa" exactly as it should have. However, when I have Chrome include the script on the actual page, it gives the following error:
Error in event handler for 'undefined': Cannot read property '1' of null TypeError: Cannot read property '1' of null
Obviously, the first part of my line returns "null" because it does not find a match, and taking [1] of "null" doesn't work.
I figured I might not be doing the match on the source code, but when I let the script output document.documentElement.innerHTML to the console, it outputs the complete source code.
I see no reason why this regex fails, so I must be overlooking something very silly. Does anyone else see the problem?
All help appreciated,
Kenneth
You're putting your regular expression inside a string. It should not be inside a string.
var world = document.documentElement.innerHTML.match(/boardid=[0-9]+">([A-Z][a-z]+)( - Trade){0,1}<\/a>/i)[1];
Another thing — it appears you have a document object, in which case all this HTML is already parsed for you, and you can take advantage of that instead of reinventing a fragile wheel.
var element = document.querySelector('a[href*="boardid="]');
var world = element.textContent;
(This assumes that you don't need <=IE8 support. If you do, there remains a better way, though.)
(P.S. ? is shorthand for {0,1}.)

Get object by attribute in IE8

The following line works fine in IE9 but is giving me a 'Object doesn't support this property or method' error in IE8.
var id = $("td[title='Project Documents']");
Is there an IE8 compatible way to get an object based on a specific attribute?
According to the docs what you have should work. Try this:
$("td[title]").filter(function () {
return 'Project Documents' === $(this).attr('title');
});
It could be that the capitalization or whitespace chars do not match exactly.
I'd be tempted to test this on a title without any whitespace in the title to test to see if it is actually this causing the problem.

Categories