简体   繁体   中英

Checking URL to replace string

I'm trying to replace the text in an header tag in a specific page by checking to see if that URL contains a unique string, then changing the header tag to another word. This is my code:

jQuery(function($) {
    var hasQueryString = document.URL.indexOf(‘shop/?orderby=date’);
    if (hasQueryString > 1) $(‘h1’).html(‘New’);
});

So my site is www.site.com, and when the user is on www.site.com/shop/?orderby=date, I want the text in the h1 tag to be replaced with 'new'. I'm using WordPress and trying and enqueue the script (which I'm pretty sure I've done correctly) but it's not working. What am I missing?

I suspect that you're calling this code prior to the completion of the page loading and there is no h1 node to populate with text. Or does your matched string start at char 0 or 1? why are you looking for > 1 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM