I'm running a code that displays content based on the url. If the user is in page X, the page displays the content number 1 and hides number 2, if he's in page Y, it displays the content number 2 and hides number 1. It works, but I would like to add a second URL to be checked.
My code is as follows:
jQuery(document).ready(function($){
var url = document.location.href;
if(url == "http://w.com/") {
$(".banner1").show();
$(".adsenselistingtop").show();
$("#leheaderbanner").hide();
$("#showhide-in-home").hide();
} else {
$(".banner1").hide();
$(".adsenselistingtop").hide();
$("#leheaderbanner").show();
$("#showhide-in-home").show();
}
});
I tried using something like this, but it didn't work:
if(url == "http://w.com/" && url == "http://w.com/index.php") {
Use OR instead of AND as the URL can be either of them.
if(url == "http://w.com/" || url == "http://w.com/index.php")
Related
I have a significant amount of external links on my website in this format:
website.com/product/[variable]
I need these links to somehow pass through “myaffiliatelink.com” before being redirected to website.com/product/[variable].
Is this possible using .htaccess or Javascript?
I looked into using .htaccess, but seems I would need to do this individually. Is there a way to set a rule such that any external link using "website.com/product/[variable]" should pass through "myaffiliatelink.com" first?
// catch every click on the page
document.addEventListener("click", e => {
const target = e.target;
if (target.tagName === 'A' && target.href.indexOf("website.com") !== -1) {
// prevent the <a> tag from navigating
e.preventDefault();
const lastSlash = target.href.lastIndexOf('/');
if (lastSlash > 0) {
const variable = target.href.substring(lastSlash + 1);
// use the commented code below, or a window.open
//location.href = "https://myaffiliatelink.com/"+variable;
// for demonstration
console.log("https://myaffiliatelink.com/" + variable);
}
}
})
Product
<p>should pass through "myaffiliatelink.com"</p>
Trying to do the following:
1) User is on page: https://www.example.net/value1/value2/value3/catid,xy somepages have value of catid=xy/value4/value5/value6/ - Category page
2) User clicks on LINK
I would like user on page catid,xy to click LINK and the URL string of catid,ab in the href to be automatically replaced with xy onclick.
Or redirected to page: https://www.example.net/index.php?valuexyz&catid,xy on click.
Simply put I want to have the URL string of catid change based on current page cat id.
<script>
if (window.location.href.indexOf("catid") > -1) {
{
document.getElementById("PostingAd").onclick HERE == window.location.href = 'https://www.example.net/index.php?value1value2value3&.catid';
}</script>
I don't know how to include catid as it keeps changing on every page
NOTE: CATID is not necessarily xy or 12 or xyz it can be 1029 or 123 or hello etc.... also... Catid can be in format of "catid=" and "catid,"
UPDATE TO BE MORE CLEAR
they all have a cat id all i am asking is if im on URL 1 and i click on BUTTON x the button will direct me (the guy browsing on URL 1 = "www.example.com/blblblblblblblsaomasmadkasmdkfsdfsm/VALUE/slkfmsdlkfnlskdf" ) to URL b which is (www.example.com/kmfmksafmdalkf?dslf/VALUE/" for value to change based on url i am on
Something like this?
const siteURL = "https://www.example.net/value1/value2/value3/catid,xy/value4/value5/value6/"; // location.href
const siteCatId = siteURL.split("catid,")[1].split("/")[0];
document.getElementById("someContainer").addEventListener("click",function(e) {
e.preventDefault(); // remove when live
var tgt = e.target;
if (tgt.tagName==="A") {
if (tgt.href.indexOf("&catid") !=-1) {
tgt.href = tgt.href.replace(/catid([=|,]).*?$/g,function (match,oper) {
console.log(match,oper)
return "catid"+oper+siteCatId;
})
console.log(tgt.href)
}
}
})
<div id="someContainer">
CHANGES - catid=ab -> catid=xy<br/>
CHANGES - catid,abc -> catid,xy<br/>
This link does not change
</div>
NOTE if your URL has catid= then use URLSearchParams instead
This code is supposed to show/hide a text based on the page's url, but it's not working:
var pages = window.location.href;
if(pages == "page1.html"){
//display page 1 text
}
else if(pages == "page2.html"){
//display page 2 text
}
http://jsfiddle.net/yp8h2moe/1/
I tested it localy, didn't work, so I tested with jsfiddle, but unfortunately every time you save a jsfiddle, you get a new url.
UPDATE
Here is the updated code, I was able to run it specifying the file extention, but I need it to run with the url only:
<script>
var pages = window.location.href;
if( pages.split('/').pop() === 'http://akecheta.com/free-blogger-templates/' ) {
document.write('<b>Hello World 1</b>');
}
else if( pages.split('/').pop() === 'http://akecheta.com/free-blogger-templates/' ) {
document.write('<b>Hello World 2</b>');
}
</script>
THE SOLUTION
How can I insert HTML text in Javascript the right way?
The value of href is usually an absolute URL so you have to use .indexOf() as follows:
var pages = window.location.href;
if(pages.indexOf("page1.html") > -1){
//display page 1 text
}
else if(pages.indexOf("page2.html") > -1 ){
//display page 2 text
}
Alternatively you could use .split() and .pop(), assuming your urls do not have query strings:
if( pages.split('/').pop() === 'page1.html' ) {
//...
UPDATE
When using absolute URLs or larger parts thereof, you do not need to use .split() and .pop(). Bear in mind that split creates an array whereas pop obtains the last element of that array. You won't be needing this process with absolute URLs.
var pages = window.location.href;
if( pages.indexOf( 'http://akecheta.com/free-blogger-templates/' ) > -1 ) {
document.write('<b>Hello World 1</b>');
}
else if( pages.indexOf( 'http://akecheta.com/free-blogger-templates/xx' ) > -1 ) {
document.write('<b>Hello World 2</b>');
}
It doesn't work because in your jsfiddle var pages is "http://fiddle.jshell.net/_display/ "
try to console.log it and make sure to trim it too if it is needed..
In your case for page1.html etc.. you have to compare it to the full/absolute path
If your URL is http://wwww.example.com/page1.html, then you need the URL path. So, code will look something like below
var pages = location.pathname;
if(pages == "/page1.html"){
//display page 1 text
}
else if(pages == "/page2.html"){
//display page 2 text
}
I require code for checking where a user has come from or change phone number?
JQuery i have is:
$(document).ready(function() {
var referrer = document.referrer;
});
I need an If Else Statement to see:
if (user is from (testsite1.com.au))
// run referrer
else
//change phone number to 0438 789 999
Please Help
UPDATED CODE SO FAR -----
$(document).ready(function() {
var referrer = document.referrer;
});
var site = 'http://www.tp1.websyte.com.au/';
if(site == true) {
alert('Came From .websyte site');
} else {
$(function(){
$('body *').replaceText( /\b03 9532 1600\b/gi, '0438 610 584' );
});
}
In order to check if a user came from your site we can use document.referrer, note that this only works when you are moving from one site to another by a link. It will not work if a user typed in a URL website directly.
To change phone numbers on the site is simple. We just look for our phone numbers decribed the the anumber class, cycle through them and change there html contents to the number you want.
var lastUrl = document.referrer;
if(lastUrl.search("websyte") == -1) {
$( ".anumber" ).each(function() {
$(this).html("0438765876");
});
}
This means that we first get the last url, call it lastUrl. Then if it had "websyte" somewhere in that url we assume that they came from your website. In that case it returns some positive number. If we didn't find it -1 is returned. Only when we didn't find "websyte" is when we cycle through all the anumber classes and change their html content to 0438765876.
Fiddle Here
I would like to change the url of the page when the user select another page to visit. The url is dynamically replace the original one.
eg.
If user visit page 1 , the url will be book.html?page=1
If page 30 then book.html?page=30 and so on.
However, when I change the link using javascript, it falls into a infinite loop.
It seems I keep visit->change link ->visit ->change link->.... How to fix this problem?
eg. When the link change, don't access the page.
var currURL = $(location).attr('href');
var index = currURL.indexOf('?');
currURL = currURL.substring(0, index != -1 ? index : currURL.length);
// fall into loop
$(location).attr('href', currURL + '?page=' + pageNo);
You can do this pretty easily with just standard javascript.
if(location.href.indexOf('?') !== -1 && location.href.indexof('?page=') === -1)
{
var urlArray = location.href.split('?');
var newURL = urlArray[0] + "?page=" + urlArray[1];
location.href = newURL;
}