Search code examples
javascriptjqueryuserscriptscustom-data-attribute

Change the page title if a data attribute is found


I'm working on an userscript for a website. Would it be possible to change the title of the site if it finds a certain data attribute?

This is what I currently have:

if ($(document.attr('data-class="car"')) {
    $("title").text('*Car found!*');
}

Solution

  • I'm guessing that AJAX is involved, in which case you can use:

    var titleCheckTimer = setInterval (checkAndChangeTitle, 333);
    
    function checkAndChangeTitle () {
        if ($('[data-class="car"]').length) {
            document.title = "*Car found!*";
    
            clearInterval (titleCheckTimer);
        }
    }
    

    (This also works on static pages as well.)