javascript function indexOf() not working in chrome with white space in string - Stack Overflow

I want to search the string in anchor tag, When I simply search the "Happy" without space it

I want to search the string in anchor tag, When I simply search the "Happy" without space it works fine however when I search it with some space like "Happy " it does not work.

Below is the code sample:

<html>
<body>

 <a style="color:#555555" href="Happy coding!!">test</a>
<br/>

<script type="text/javascript">

var hrefTags = document.getElementsByTagName("a");
var bDayId = ""; 
var stringToSearch = "Happy coding!!";
    for(var hrefIndex=0; hrefIndex < hrefTags.length; hrefIndex ++){
bDayId = hrefTags.item(hrefIndex).href.toString(); 
document.write(bDayId+"<br/>");
document.write( bDayId.indexOf("Happy "));  
     }  

</script>

</body>
</html>

I want to search the string in anchor tag, When I simply search the "Happy" without space it works fine however when I search it with some space like "Happy " it does not work.

Below is the code sample:

<html>
<body>

 <a style="color:#555555" href="Happy coding!!">test</a>
<br/>

<script type="text/javascript">

var hrefTags = document.getElementsByTagName("a");
var bDayId = ""; 
var stringToSearch = "Happy coding!!";
    for(var hrefIndex=0; hrefIndex < hrefTags.length; hrefIndex ++){
bDayId = hrefTags.item(hrefIndex).href.toString(); 
document.write(bDayId+"<br/>");
document.write( bDayId.indexOf("Happy "));  
     }  

</script>

</body>
</html>
Share Improve this question edited Dec 20, 2011 at 13:05 Sameera Thilakasiri 9,50810 gold badges53 silver badges87 bronze badges asked Dec 20, 2011 at 7:52 Jitendra ZaaJitendra Zaa 2123 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

its because href being a URL gets URL encoded , space bees '%20' . Href with a space is an invalid href, hence use another attribute if you want to store some information in href attribute, or if you want to read and string pare href attribute with spaces, pare after proper url decoding. http://jsfiddle/Sddv6/1/

var hrefTags = document.getElementsByTagName("a");
var bDayId = ""; 
var stringToSearch = "Happy coding!!";
for(var hrefIndex=0; hrefIndex < hrefTags.length; hrefIndex ++){
  bDayId = decodeURIComponent(hrefTags.item(hrefIndex).href);      
  document.write(bDayId);    
  document.write(bDayId+"<br/>");
  document.write( bDayId.indexOf("Happy "));  
}  

Use regular expression for matching the string look at the following URL will help: http://www.tizag./javascriptT/javascript-string-replace.php

The space will be encoded as "%20" while retrieving from the link href attribute. Try something below:

bDayId = decodeURIComponent(hrefTags.item(hrefIndex).href.toString());

When I do that in Firefox, the string I get from toString() is:

file:///home/pax/Happy%20coding!!

If that's what you're seeing then of course you won't be able to find "Happy " in there. When I change the search statement to:

document.write( bDayId.indexOf("Happy%20"));

then it works fine.

Perhaps you may want to think about url-encoding the search string similar to the way your browser does it in toString():

document.write( bDayId.indexOf(encodeURIComponent("Happy ")));

This is, of course, assuming I'm right about the encoding. You didn't provide the actual output from your HTML so it's a little difficult to tell.

It would be worthwhile posting that as an edit to your question.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745445293a4628020.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信