Javascript: adding a website to favorites _ javascript skills

Source: Internet
Author: User
This article mainly introduces related information about how to add a website to favorites using javascript. For more information about how to add a website to favorites using javascript, see the following code to add a website to favorites:

First case: compatible with adding code to favorites from all browsers,Principle:Determine the browser based on the information obtained from the user navigator. userAgent. toLowerCase (). Determine whether the browser supports adding to the favorites js command. If the browser can automatically add to favorites, otherwise the system prompts ctrl + D to manually add to favorites.
The Code is as follows:

Function addFavorite2 () {var url = window. location; var title = document. title; var ua = navigator. userAgent. toLowerCase (); if (ua. indexOf ("360se")>-1) {alert ("due to limitations of the 360 browser, press Ctrl + D to manually add to favorites! ");} Else if (ua. indexOf ("msie 8")>-1) {window. external. addToFavoritesBar (url, title); // IE8} else if (document. all) {try {window. external. addFavorite (url, title);} catch (e) {alert ('not supported by your browser. Press Ctrl + D to manually add it to favorites! ') ;}} Else if (window. sidebar) {window. sidebar. addPanel (title, url, "") ;}else {alert ('your browser does not support it. Press Ctrl + D to manually add it to favorites! ');}}

Save the file as a js file and add it to the favorites folder
The Code is as follows:

The Code is as follows:

Add to favorites

Case 2: Set the js Code as the homepage and add it to the favorites folder

// JavaScript Document // Add to favorites add function AddFavorite (sURL, sTitle) {try {window. external. addFavorite (sURL, sTitle);} catch (e) {try {window. sidebar. addPanel (sTitle, sURL, "") ;}catch (e) {alert ("failed to add to favorites, please add with Ctrl + D ");}}} // set it as the homepage function SetHome (obj, vrl) {try {obj. style. behavior = 'url (# default # homepage) '; obj. setHomePage (vrl);} catch (e) {if (window. netscape) {try {netscape. security. pri VilegeManager. enablePrivilege ("UniversalXPConnect");} catch (e) {alert ("this operation was rejected by the browser! \ N enter "about: config" in the address bar of the browser, press enter \ n, set the value of [signed. applets. codebase_principal_support] To 'true', and double-click it. ");} Var prefs = Components. classes ['@ resolve (Components. interfaces. nsIPrefBranch); prefs. setCharPref ('browser. startup. homepage', vrl );}}}

Use

Set as homepage to add to favorites

Case 3: Add code to favorites in js

To gather users and maintain traffic, many websites have buttons such as "set as Homepage" and "add to Favorites". The code for adding a favorite to js is as follows:

Script function addfavorite () {if (document. all) {window. external. addFavorite ('HTTP: // www.jb51.net', 'foot home');} else if (window. sidebar) {window. sidebar. addPanel ('feet home', 'HTTP: // www.jb51.net', "");} scriptAdd to favorites!

Result test:This code is valid for IE6 + and FireFox, and Chrome is invalid!

The above is the js Code implementation to set as the home page and add to the favorites function, I hope everyone will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.