简体   繁体   中英

Make default homepage for IE9 javascript

I am trying to make a default homepage script using the famous script for IE but I'm trying to add it in a jquery function.

So this works:

<a href="#" onClick="document.body.style.behavior='url(#default#homepage)';
  document.body.setHomePage('http://www.google.com');">
    Click here to make My Site your default homepage
  </a>

but this doesn't:

<a href="#" onClick="">
    Click here to make My Site your default homepage
  </a>

$('a').click(function() {

 document.body.style.behavior='url(#default#homepage)';
  document.body.setHomePage('http://www.google.com');

});

What I'm I doing wrong?

EDIT: Please note the on click function doesnt work for IE9. IE6,7 and 8 it works.

Your code works fine in IE (tested in IE 7). Check DEMO here.

But it seems like you cannot do the same in FF. Read about it here

Alternatively, you can provide an instruction in FF on how to change.. See DEMO here

With jQuery 1.7 use the .on() function:

$(document).ready(function() {

    $("a").on("click", function(event){
       document.body.style.behavior='url(#default#homepage)';
       document.body.setHomePage('http://www.google.com');

    });
});

jsFiddle: http://jsfiddle.net/nKkNV/

By using this: $('a') you are targeting all links on the page.

Try giving the anchor tag an id attribute, then change your jQuery selector to look for that attribute, like this (also remove the onClick attribute altogether):

<a href="#" id="makeDefault">
    Click here to make My Site your default homepage
  </a>

$('a#makeDefault').click(function() {
  document.body.style.behavior='url(#default#homepage)';
  document.body.setHomePage('http://www.google.com');
});

你在两个版本中做错的一件事是,你有一个链接文本链接“点击这里使我的网站成为你的默认主页”,指向当前页面的开头。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM