Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by 9apit
at 2024-11-19 11:00:02
Point:500 Replies:11 POST_ID:828816USER_ID:11723
Topic:
JavaScript;;
Hello expert,
Doing some basic investigations using JavaScript Arrays
in the script below after I got the array to write
Doing some basic investigations using JavaScript Arrays
in the script below after I got the array to write
to the page
I was going to get the index number to write. Added
var y = myColors.indexOf("green");
but this breaks the code.
I have checked the syntax and it seems to be ok.
What anm I doing wrong?
Thanks
Allen in Dallas
I was going to get the index number to write. Added
var y = myColors.indexOf("green");
but this breaks the code.
I have checked the syntax and it seems to be ok.
What anm I doing wrong?
Thanks
Allen in Dallas
Attachment:JS-array-loop.html
Expert: ModeIT replied at 2024-11-19 20:46:42
Thread Cleansed.
MIT
MIT
Expert: Chris Stanyon replied at 2024-11-19 14:50:26
Hey 9apit - now you've said that you're using IE8 (an out of date browser), it makes more sense. The indexOf function is not supported in IE8, so it causes an error. IE is very unforgiving with Javascript errors, and a single error will cause your whole script to fail.
Couple of options. If it's just the index you need, then you already have that in the i variable inside the loop. Another option is to define your own indexOf function. Havea read here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
The third option (and my favourite) is to start using jQuery. It may sometimes seem like overkill to include the library, particularly if you have very simple javascript requirements, but the footprint is so small, and it solves all these cross-browser issues. To loop through your array:
Couple of options. If it's just the index you need, then you already have that in the i variable inside the loop. Another option is to define your own indexOf function. Havea read here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
The third option (and my favourite) is to start using jQuery. It may sometimes seem like overkill to include the library, particularly if you have very simple javascript requirements, but the footprint is so small, and it solves all these cross-browser issues. To loop through your array:
To get an index of an array element
@duncan - you didn't give any answers - you simply asked what browser version. You also said it was to do with DOCTYPES and closing tags!!
Expert: duncanb7 replied at 2024-11-19 13:09:19
thanks you agree that is related to browser issue and not just code .
Duncan
Duncan
Author: 9apit replied at 2024-11-19 13:02:42
Hello Duncan,
Yes it is browser related.
The code works in FireFox
but not in the IE 8 installed on
my corporate laptop.
Allen
Yes it is browser related.
The code works in FireFox
but not in the IE 8 installed on
my corporate laptop.
Allen
Expert: duncanb7 replied at 2024-11-19 12:58:28
Dear 9apit,
Christ found the result already but the issue is not related
to your modified post code, JS-array-loop.html, and you said you get blank page(or not working) after Chris correcting your code.
Is it related to browser ? Am I right ?
Please advise
Duncan
Christ found the result already but the issue is not related
to your modified post code, JS-array-loop.html, and you said you get blank page(or not working) after Chris correcting your code.
Is it related to browser ? Am I right ?
Please advise
Duncan
Author: 9apit replied at 2024-11-19 12:25:37
Hello Chris,
When i tried it in FireFox it performed just as stated.
Still get blank page in IE 8.
Thought it might be JavaScript turned off but I checked
Internet Options, Secuity and JavaScript is not disabled.
Besides when I comment out the indexOf statement
the rest of the code works, so JavaScript would have to be
working for the rest of the code to work.
When I uncomment out the
var y =... line the page goes blank again. The IE 8 browser
is on a lap top at a corporation where they have really
locked down the security. So it maybe something the
IT dept, did to restrict script access. For instance,
when an IE session is first begun before the page displays
a message appears: "To help protect your security
Internet Explorer has restricted this web page from running script
or Active X controls that could access to your computer. Click
here for options. Then when the link is clicked a second warning is given.
But even when the browser is told twice that scripting is ok the
page returns blank.
Thanks.
Allen in Dallas
When i tried it in FireFox it performed just as stated.
Still get blank page in IE 8.
Thought it might be JavaScript turned off but I checked
Internet Options, Secuity and JavaScript is not disabled.
Besides when I comment out the indexOf statement
the rest of the code works, so JavaScript would have to be
working for the rest of the code to work.
When I uncomment out the
var y =... line the page goes blank again. The IE 8 browser
is on a lap top at a corporation where they have really
locked down the security. So it maybe something the
IT dept, did to restrict script access. For instance,
when an IE session is first begun before the page displays
a message appears: "To help protect your security
Internet Explorer has restricted this web page from running script
or Active X controls that could access to your computer. Click
here for options. Then when the link is clicked a second warning is given.
But even when the browser is told twice that scripting is ok the
page returns blank.
Thanks.
Allen in Dallas
Expert: duncanb7 replied at 2024-11-19 11:54:32
I agree Christ I run it at my side and I can output the result as Chris said
Is it browser issue, 9apit ? What version of your older browser you are using ?
and it might be related to HTML/XHTML' s DOCTYPE for closing tag issue
that I am not sure without your more information about your browsers
Duncan
Is it browser issue, 9apit ? What version of your older browser you are using ?
and it might be related to HTML/XHTML' s DOCTYPE for closing tag issue
that I am not sure without your more information about your browsers
Duncan
Accepted Solution
Expert: Chris Stanyon replied at 2024-11-19 11:52:33
500 points EXCELLENT
Both the files you've attached do exactly what they're supposed to - they write out a list of colours, followed by 'mauve'. The first file then writes out -1 because you call the indexOf before setting the array, while the scond file writes out 1, which is correct - 'green' is at position 1.
Don't see a problem.
Don't see a problem.
Expert: duncanb7 replied at 2024-11-19 11:50:30
What you mean you get blank page ? could you explain it more ?
I tried you both html code,
I got this
before indexOf is removed to after the array defined
I tried you both html code,
I got this
before indexOf is removed to after the array defined
after indexOf is removed to after the array defined
Author: 9apit replied at 2024-11-19 11:16:39
Hello Chris,
Before the page wrote out s list of colors. When I included the indexOf statement
the page was blank.
So the index of statement was moved to after the array (attachwed file) variable but
the same result, a blank page, is achieved.
Allen
Before the page wrote out s list of colors. When I included the indexOf statement
the page was blank.
So the index of statement was moved to after the array (attachwed file) variable but
the same result, a blank page, is achieved.
Allen
Expert: Chris Stanyon replied at 2024-11-19 11:09:09
What do you mean by 'breaks the code'
In your page you are calling this:
var y = myColors.indexOf("green");
before you ever setup the myColors array, so it will always return false (-1)
In your page you are calling this:
var y = myColors.indexOf("green");
before you ever setup the myColors array, so it will always return false (-1)