Reputation: 115
I want to redirect the user automatically into another page if this text exist 'Shopping Cart is Empty'. I found this jQuery :contains() Selector and test on it but It didn't work. Here's my code:
<div class="page-title">
<h1>Shopping Cart is Empty</h1>
</div>
<script>
$( "div.page-title:contains('Shopping Cart is Empty')".window.location.replace("http://www.another-page.com");
</script>
Upvotes: 1
Views: 447
Reputation: 4692
Of course make sure your Codes are inside jQuery load function or something like.
$(function(){
..Your Code here...
})
Then, you may try to use the indexOf()
to be more specific.
Something like:
if( $("div.page-title h1").text().indexOf('Empty') !=-1 ){
window.location.href="http://www.goodle.com";
}
So, all in all it will be something like:
$(function(){
if( $("div.page-title h1").text().indexOf('Empty') !=-1 ){
window.location.href="http://www.goodle.com";
}
})
Hope it helps!
Upvotes: 1
Reputation: 4388
you can also use contains
like this
<script>
if ($( "div.page-title:contains('Shopping Cart is Empty')").length > 0) {
window.location.replace("http://www.another-page.com");
}
</script>
the content
element returns all the divs element objects that contains the specified string. so check for length then redirect.
Upvotes: 0
Reputation: 76
if($('.page-title h1')text()=="Shopping Cart is Empty"){
window.location.replace("http://www.another-page.com"); }
Upvotes: 1
Reputation: 57105
$(function () {
var len = $("div.page-title h1").filter(function () {
return this.innerHTML === "Shopping Cart is Empty";
}).length;
if (len > 0) {
window.location.replace("http://www.another-page.com");
}
});
Upvotes: 0
Reputation: 73976
You can do this:
// Call the function when DOM is ready
$(function () {
// Check if page title has a text using length
var len = $("div.page-title h1").filter(function () {
return $(this).text() === "Shopping Cart is Empty";
}).length;
// If the text is there on the page, redirect to another page
if (len > 0) {
window.location.replace("http://www.another-page.com");
}
});
Upvotes: 3
Reputation: 15709
Try this:
if($("div.page-title :contains(Shopping Cart is Empty)").length > 0){
window.location.href = "http://www.another-page.com";
}
Upvotes: 1