Error: Unable to get property 'lastIndexOf' of undefined or null reference

Saikat M
Saikat M used Ask the Experts™
on
Hi,
i'm getting this error on IE10 browser for but this is happening only in one instant..
MasterScriptIns = MasterScriptIns.nextSibling;
    var InnerScript = MasterScriptIns.innerHTML;
    var Index = InnerScript.lastIndexOf("WebForm_AutoFocus('");
    if(Index == -1)
    {
        window.focus();
        return false;
    }


please advice
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Freelance programmer / Consultant
Commented:
Does this work?  

var InnerScript = MasterScriptIns.innerHTML;
if(InnerScript != null)
{
    var Index = InnerScript.lastIndexOf("WebForm_AutoFocus('");
    if(Index == -1)
    {
        window.focus();
        return false;
    }
}
Hi

This means that MasterScriptIns.innerHTML returns something different than string.

You have to make a check after each assignment to see what have you gotten.

you could restate this as follow:
MasterScriptIns = MasterScriptIns.nextSibling; // you have to check after this assignment.
var InnerScript = MasterScriptIns.innerHTML || ""; // in the case InnerScript will be empty string and won;t fire the error

Open in new window


HTH

Ivo Stoykov

Author

Commented:
Thanks a ton. It worked out.
AndyAinscowFreelance programmer / Consultant

Commented:
A rapid response, A correct answer (with code) that works. Then why only a 'B' grade?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial