最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

safari extension - Javascript set title onload? - Stack Overflow

programmeradmin0浏览0评论

Is there anyway to set the title of a website via JS onload? I wrote this but I'm not sure where it's incorrect:

function my_code(){
document.title = "The new title goes here.";
}

window.onload=my_code();

The page title is static by the way.

Edit: The reason why I want to do it this way is because I'm writing a Safari Extension for a website that does not include tags so I wanted to insert one via JS.

Is there anyway to set the title of a website via JS onload? I wrote this but I'm not sure where it's incorrect:

function my_code(){
document.title = "The new title goes here.";
}

window.onload=my_code();

The page title is static by the way.

Edit: The reason why I want to do it this way is because I'm writing a Safari Extension for a website that does not include tags so I wanted to insert one via JS.

Share Improve this question edited Sep 4, 2011 at 17:55 Charlie asked Sep 4, 2011 at 17:48 CharlieCharlie 11.8k19 gold badges87 silver badges140 bronze badges 3
  • why do you want to do this? the user won't be able to see it. – Daniel A. White Commented Sep 4, 2011 at 17:50
  • Why not just put the title in the title tag as normal? – Jamie Dixon Commented Sep 4, 2011 at 17:50
  • I'm writing a safari extension that changes a website's layout and there is no title tag, so I wanted my script to add one via JS onload. – Charlie Commented Sep 4, 2011 at 17:50
Add a ment  | 

3 Answers 3

Reset to default 6

You don't need to wait for the load event.

Just write document.title = ... anywhere.

If you want to change the title after the page loaded, you have to assign a function reference to onload (your function assigns the return value of my_code):

window.onload = my_code;

However, you most certainly can set the title without waiting for the load event.

I'm not familiar with Safari extensions, but you should also make sure that window actually refers to the page's window.

solved your question

script:

function load_title(){
    var title = document.getElementsByTagName("title")[0];
    var text_title = "SUNDAGEEKS ツ The aspiring white-hat hacker/security awareness playground ≪ SUNDAGEEKS :: INDOXPLOIT";
        document.title = text_title;
}
var title_js = document.createElement("title");
    window.onload = load_title();
    document.getElementsByTagName("head")[0].appendChild(title_js);
发布评论

评论列表(0)

  1. 暂无评论