全てIEが悪い。いや、IEを甘く見てた無知な自分が悪い。
以下みたいな感じでaタグからhrefを取得しようとして困った
<html lang=ja> <head> <script type="text/javascript"> window.onload = function() { var btn = document.getElementById('btn'); var aTags = document.getElementsByTagName('a'); btn.onclick = function() { for (var i=0; i<aTags.length; i++) alert(aTags[i].href); } } </script> </head> <body> <a href="http://www.example.com/%s/hogehoge/">Link</a> <input type="submit" value="View URL" id="btn"> </body> </html>
IEだと「aTag[i].href」のところで、「引数が無効です」と怒られる。
でもChrome(Webkit?)だと問題ないんだこれが。
適当にエンコードして回避しようとしたが、取得しようとした時点で怒られたり。
というわけでいろいろ調べた結果の回避策。
vAttrValue = object.getAttribute(sAttrName [, iFlags])
[iFlags] Optional. Integer that specifies one or more of the following flags:
0: Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found.
1: Performs a case-sensitive property search. To find a match, the uppercase and lowercase letters in sAttrName must exactly match those in the attribute name.
2: Returns attribute value as a String. This flag does not work for event properties.
4: Returns attribute value as a fully expanded URL. Only works for URL attributes.
とりあえず、getAttributeに第二引数とか知らん。
でも修正したら動いたのでまあ気にせず。
<html lang=ja> <head> <script type="text/javascript"> window.onload = function() { var btn = document.getElementById('btn'); var aTags = document.getElementsByTagName('a'); btn.onclick = function() { for (var i=0; i<aTags.length; i++) alert(aTags[i].getAttribute('href',2)); } } </script> </head> <body> <a href="http://www.example.com/%s/hogehoge/">Link</a> <input type="submit" value="View URL" id="btn"> </body> </html>