Need help converting jquery to javascript, add to home screen on apple
I have written some jquery to get around this problem: iPhone "Bookmark to Homescreen" removes cookies and session?
Basically, if you use java开发者_如何学Pythonscript to make the add to homescreen kit launch links it won't bump you into safari (as it normally does, and which is annoying as hell because it kills any cookie information you had)
<script>
$(function(){
$('a[href]').click(function(e){
e.preventDefault();
document.location.href = $(this).attr('href');
return false;
}) ;
});
</script>
Anyway I don't want to use jquery on this site because that is the only piece of code i use jquery for.
Can someone help me build something in straight javascript that will do it? It only has to be compatible with safari, if there's something to detect the safari browser that'd be good too because you can't add to homescreen on any other platform that we are targeting
var anchors = document.links;
for (var i = 0; i < anchors.length; i++) {
anchors[i].onclick = function(){
alert("contact!");
e.preventDefault();
document.location.href = this.href;
return false;
}
}
untested but that should work.
Edit
ok, tested
http://jsfiddle.net/7q6xx/2
Edit 2
Change it to use the links collection per RobG's suggestion.
As for Joseph's answer, but using the links collection instead:
var links = document.links;
for (var i=0, iLen=links.length; i<ilen; i++) {
links[i].onclick = function(){
e.preventDefault();
document.location.href = this.href;
return false;
}
}
Give him the rep points.
精彩评论