JavaScript HTML Title Scrolling
A history lesson

I feel obligated to note that similar functionality (in the page body) has already been implemented in native html using the &lt;marquee&gt; tag (mdn). This tag has been deprecated for forever though, because it is deemed a usability nightmare and visually distracting without a cause.

The same issue applies to changing the title of the page. It distracts the user, and may annoy the user. Users can already view the entire page title by hovering over the tab, but most people do not care about what the title is at all.

Avoid onload (and other on... definitions)

Don't use the on... family of attributes to create listeners for events. Your code does not operate in a void. There are other scripts, some of which you do not have control over. When you assign to an on... attribute, you override the previous assigned function. This, in turn, creates hard to debug problems.

Instead, use .addEventListener(..) (mdn). You can add and remove as many listeners as you want without having to worry about other code.



var clicker = document.getElementById('clicker');

clicker.onclick = function () {
  console.log('First handler');
}
clicker.onclick = function () {
  console.log('Second function');
}
The problem with onclick (and onload, and other on... attributes):

&lt;a href="#" id="clicker"&gt;Click me!&lt;/a&gt;




Custom parameters

You are correctly using attributes with the data- prefix for your custom attributes. If we want to use such attributes though, we usually put them on the element we are manipulating, not a random script tag. In this case, it's probably better to put it on the title tag. This will also simplify your code a lot, since you only need to grab the title tag and do not have to worry how you load in your code, or if any other code is using your custom attributes.

Optimalisation of housekeeping in interval callback

You are currently using the following code for housekeeping in your interval callback

i++;

if (i === title.length) {
    i = 0;
}


You could instead leverage the modulo operator to simplify the code.

i = (i + 1) % title.length;


Separator

You are calculating your title length without the separator. This means that the title will not complete a full round and instead will jump back to the beginning when -- is at the start.

Various other things

It is always a good idea to use IIFE (Immediate Invoked Function Expressions) to encapsulate your code. This prevents other scripts from interacting with your variables and functions. An IIFE looks something like the following, and is invoked with the variables it depends on (e.g. a javascript library such as jQuery):

(function () {
  // Your code
})();


You have on inconsistent space in your function declaration at setInterval(function () {. Your variable title_ref is not written in camelcase, while the rest of the variables and functions in your code are written that way.

Other than that your code looks clean. Your variable names are clear and make it easy to understand what they contain.