I write a form submit thread with the attr method:
var href = a.attr('href');
if(href =='') return;
if(href.startsWith('http') || href.startsWith('//:')){
a.attr('href',href.trim());
if(href.indexOf(location.hostname) > -1){
return;
}
if(href.indexOf('<?php echo $_SERVER['SERVER_NAME']; ?>') > -1){
return;
}
a.attr('href','go.htm?uri='+href);
a.attr('rel','nofollow');
}
However, it returns the URL without ?, for example, I want to return https://example.abc.com/?ref=abc, the return result will be https://example.abc.com/ref=abc without "?" the question mark.
I'm struggling with solving this and I appreciate any helpful tips.
You can use split function string. Split your URL with "?" and take first element.
var yourRequiredLink = href.replace("?", '')
Related
I want to put in my HTML/PHP a script for redirection if the website name does not match my word. I tried to use this code, but it doesn't work.
WITH REGEX
if (window.location.hostname !== 'myword.org'){
window.top.location.href = 'http://redirecttomysite.org';
}
var website = window.location.hostname;
var internalLinkRegex = new RegExp('^((((http:\\/\\/|https:\\/\\/)(www\\.)?)?'
+ website
+ ')|(localhost:\\d{4})|(\\/.*))(\\/.*)?$', '');
WITHOUT REGEX
if (website !== 'myword.org'){
window.top.location.href = 'http://redirecttomysite.org/forum';
}
try
if (website.indexOf('myword.org') == -1){
window.top.location.href = 'http://redirecttomysite.org/forum';
}
if(window.location.href.indexOf("myword.org") == -1) {
window.location = 'http://redirecttomysite.org';
}
simply as below
if (!(/stackoverflow\.com/i.test(location.hostname))){
...
similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
I want that the outbound and iframed links on my site should append the url parameters from the url of the current page.
So, lets say I have a page mysite.com and I have a link on that page that leads to somesite.com (or I have iframed somesite.com on mysite.com)
Now I want that if someone arrives at my site with some url strings in it (like utm_source etc) like: mysite.com/?parameter1=value1¶meter2=value2 , I should be able to append some or all parameter values from the mysite link to the somesite link on that site (or iframed on it).
So, say I wanted the somesite link to have to parametr2's value from the mysite link's url, in its another parametr's value. So, lets say I want the somesite link to be : somesite.com/?a1=[Parameter2'sValueHereFromMySiteLink]
I need to be able to fetch any value from any parameter from the source link and append that value anywhere on the somesite link.
I searched the internet and found this solution (but it didn't work):
$('a.my-class').each(function() {
var href = $(this).attr('href')
if(href !== undefined) {
var url = document.URL;
int index = url.indexOf("Source=");
if(index != -1){
url = url.substr(index + 7) // 7 = length of "Source="
indexx = url.indexOf("&");
if(indexx != -1)
url = url.substr(0, indexx + 1);
// now url contains the value
href = href.replace("website", url);
$(this).attr('href', href);
}
}
});
Any help would be appreciated. Thanks a lot.
This code gets every field and value from $_GET and creates a text of if:
$text = '?';
$c = 0;
foreach($_GET as $k => $v){
$text .= ($c>0) ? "&$k=$v" : "$k=$v";
$c++;
}
$text = ($text=='?') ? "" : $text;
echo $text;
Using your example mysite.com/?parameter1=value1¶meter2=value2, the variable $text has this value ?parameter1=value1¶meter2=value2
So you can use it like this:
<a href=some_url<?php echo $text; ?>>SOME_TEXT</a>
I have an HTML form with multiple select dropdowns (most of the jQuery snippets I've found online talk about multi-selects which are different, i.e. checkboxes.)
How can you use jQuery to collect/join the values from all the dropdown fields, and separate them with a symbol i.e. comma or plus sign? Then, send to URL (WordPress tag search).
I've tried this code and it doesn't work:
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var tags = $(".option").map(function() {
return ($(this).text() == "") ? null : $(this).text(); // ignore default
// return $(this).text(); // include all values
}).join("+");
window.location = "<?php echo home_url( '/' ); ?>/tag/" + tags;
return false;
});
});
</script>
Here's the latest version, with .get added, still no luck:
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var tags = $(".option").map(function() {
return ($(this).text() == "") ? null : $(this).text(); // ignore default
// return $(this).text(); // include all values
}).get().join("+");
window.location = "<?php echo home_url( '/' ); ?>/tag/" + tags;
// return false; // tried this on and off
});
});
</script>
A couple things: 1) added .get() to get access to .join(), and 2) limited the return to only selected options that are not blank:
jQuery(document).ready(function() {
jQuery("#submit").click(function(evt) {
evt.preventDefault();
var tags = $(".option").map(function() {
return (this.selected && $(this).text() != "") ? $(this).text() : null;
}).get().join("+");
window.location = "<?php echo home_url( '/' ); ?>/tag/" + tags;
});
});
And for good measure, pulled in the event object and prevented the default action.
JSFiddle: http://jsfiddle.net/jKGP8/
You should use get() in combination with jquery map function, otherwise you receive not just array but jQuery object
var tags = $(".option").map(function() {
return ($(this).text() == "") ? null : $(this).text(); // ignore default
}).get().join("+");
$("#result").html(tags);
http://jsfiddle.net/tx8HT/
With on click you should just replace your old var tags = .. string with new tag calculation.
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var tags = $("#form .option").map(function() {
return ($(this).text() == "") ? null : $(this).text(); // ignore default
}).get().join("+");
window.location.href = "<?php echo home_url( '/' ); ?>/tag/" + tags;
return false;
});
});
</script>
Looks like your jQuery selector is wrong, should be "option" rather than ".option" (your code is looking for elements with the class "option"). Also, this will concatenate all select option text values, not just the ones that the user has selected. It is not clear if this is what you are after, but I thought I would give you a heads up.
Here I am stuck with a scenario where I want to edit href of anchor tag before redirect. My scenario is if href attribute of anchor tag do not contain QueryString then append previous page query string to href means, suppose my current page url is xyz?x=2&y=4
Current href
href="local/services"
After Edit
href="local/services?x=2&y=4"
I tried.
HTML:
test link
JavaScript
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1)
obj.setAttribute('href') = url + window.location.search;
}
also tried
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1)
window.location.href = url + window.location.search;
}
but no success.
Please suggest a solution.
Thanks.
The parameters for 'setAttribute' are incorrect.
element.setAttribute(name, value);
Courtesy MDN : Element.setAttribute
Jed Burke pointed to your mistake. So you should use something like this:
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1) obj.setAttribute('href', url + window.location.search);
}
or even simpler:
function appendString(obj) {
if (obj.href.indexOf('?') != -1) obj.href += window.location.search;
}
You can use JQuery for this:
$('.myLink').click(function() {
$(this).attr("href", this.href + "?param=1");
});
I want do something: if something type url address like: www.mydomain.com/search(without request part), I want do something with javascript, fill with the url like www.mydomain.com/search?search=car, but nothing happened in my code.
<script>
var curent_url = document.URL;
if(document.URL.indexOf("?") < 0){ //if(!document.URL.match(/\?/)){
//alert('ok');
document.URL = curent_url+'?search=car';
}
</script>
Instead of document.URL, check for an empty document.location.search:
if (document.location.search.length === 0) {
// empty query string... append your part to doc
document.location.href = document.location.href + "?search=car";
}
Examine the document.location object in your console to see what else it offers. There's usually no need to parse out document.URL or document.location.href directly.
console.dir(document.location);