Question

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.

Was it helpful?

Solution

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

Just write document.title = ... anywhere.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top