javascript compatibility chrome, safari, and opera on string search -
I am making my first JavaScript application and I have seen that it only works in Firefox (I do not test IE can). I have created this software to do some practice, in some words it allows searching for a given word in a given text, and renders it on a div block, the original text highlighting word search ( If it exists) and also tells
the variable string is the text where the search is being performed, the variable target is the search term For search result collection inside string The variable is the result .
The problem is that I feel completely searching the code every time in Firefox, the word is present in the second lesson (on the basis of WebKit?) It only gets the word once.
Here is the JavaScript code:
function wordfinder () {'strict use'; Value, target = string.match (target, 'g'), marked = string.replace (target, 'and lier; var string = document.getElementById (' paste '). Value, target = document.getElementById (' target ' ). Span class = "found" gt; '+ target +' , 'g'); If (result === faucet) {document.getElementById ('Result'). InnerHTML = 'Your word was not found in content; Document.getElementById ('string'). InnerHTML = '& lt; H2 align = "center" & gt; No results to display & lt; / H2 & gt; '; } And {document.getElementById ('Result'). InnerHTML = ('word' + target + 'was found' + result.length + 'times.'); Document.getElementById ('string'). InnerHTML = marked; }}
And here's HTML:
& Lt; Textarea name = "text" rows = "10" cols = "100" id = "paste" & gt; & Lt; / Textarea & gt; & Lt; Br> Paste the word you want to search for: & lt; Br> & Lt; Input name = "search" type = "text" id = "target" /> & Lt; Br> & Lt; Button type = "button" onclick = "wordFinder ()" & gt; Search. & Lt; / Button & gt; & Lt; / Form & gt; & Lt; H4 id = "result" & gt; & Lt; / H4 & gt; & Lt; Div id = "style" class = "layout" & gt; & Lt; P id = "string" & gt; Results will appear here & lt; / P & gt; & Lt; / Div & gt; & Lt; / Body & gt; & Lt; / Html & gt;
Thanks in advance for help
treated this line Has been done differently:
result = string.match (target, 'g'),
note that string.match < / Code> Single
. You can create this RegExp
argument (see). You pass two arguments, which means that the first is taken as the regx, which means that it will not give you a global match, as long as target
is not something like this < Code> / foo / g RegExp
object so that you can match it globally:
result = string.match (new regxp (target, 'g')) ,
I tested Opera 12 (Presto), Opera 26, IE 11 and Chrome 40, and all of them behave the same way, give a single match for their original code, only Firefox isolated Behaved in a way - appears to violate the EcmaScript 5.1 specification.
Comments
Post a Comment