iFrame differentiating Mainpage from everything else using Regular Expression Javascript - javascript

I want to return a value in a function depending on the Location URL from within an iFrame.
The value is 1 if the iFrame is on the main site page, and 2 if anywhere else on the site. Currently it only returns 1.
I'm new to RegEx and have a feeling it is my syntax.
Any help would be hugely appreciated!
Here is what I have so far:
var LocationURL = document.referrer;
re = new RegExp("/^(http:\/\/)?(www.)?(website.co.uk/)[a-zA-Z0-9/.]+$/");
bFound = re.test(LocationURL);
function getJoinPageLoc()
{
return (bFound == true)? 2 : 1;
}
console.log(getJoinPageLoc());
I am trying to allow it to start with http://www. or just www. or nothing.
I am assuming that any other location on the site will start with / so I am using website.co.uk/ as an absoloute (obviously website will be the actual website), followed by anything else (to make sure it's not the main page).
I hope this makes some sense!
Edit:
Still trying. Have tried this also but still not working:
re = new RegExp("/^(https?:\/\/)?(www\.)?website\.co\.uk/([a-zA-Z0-_9/\.]+)&/");

The problem was with document.referrer picking up the wrong frame >_<
Have had to, insteadm pass in a parameter through the top location and work this the other way round. Which in retrospect is much simpler >_<

Related

Redirect automatically all links from one basic-URL to one other URL (HTML, JS)

First: English is not my motherlanguage, so I hope I am able to describe my problem though in a way you understand it and maybe even give me a solution.
PROBLEM:
My old affiliate partner did not longer work so thousands of these product-links are also not longer working. To avoid replace them all with my hands, I think there must be a way with JavaScript (or anything like that, unfortunately I am a coding-noob) to redirect all of these links to ONE new link (in my case the amazon-startpage).
EXAMPLE:
Each links from "http://www.example-domain.xy" (like example-domain.xy/product-z1 , example-domain.xy/product-z2 and so on) shall be redirected to "https://www.amazon.com/start-page-with-myRef"
SOLUTION:
So which code is neccessary to do this? I use Wordpress, it may help you. I guess, some JS in the functions.php could handle this.
Thank you honestly a lot!
Your best bet is to use a WP plugin to rewrite/replace all of the URLs with the new one. This approach you will only need to rewrite them once and not have to worry about them in the future. Just be cautious.
Something like the Search & Replace plugin might do the trick, otherwise try Search Regex.
Here's the JavaScript you would use:
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
var href = links[i].href;
if (href.startsWith("http://www.example-domain.xy"))
links[i].href = "https://www.amazon.com/start-page-with-myRef";
}
Product-A<br/>
Product-B<br/>
Product-C<br/>

Truncate a string after match a specific word in smarty or javascript/jQuery

I am working on a website but somehow I have sometimes little bug coming from the add of content after the end of some string.
For example :
style="background-image:url({$sub.image});" return
style="background-image:url(http://blablalba.fr/phoenix.jpg%3Fv%3D1);"
As you can see there is %3Fv%3D1 after the end on the link (after .jpg). I don't know where this is coming from and I'd like to erase it.
So my question is : in smarty is there a way to truncate a String after matching a word ?
And if it is not possible, what is the easiest way in Js/jQuery ?
I tough of this Regex to match the correct string : /^(.*?\.jpg)/
So it lead me to this :
$('.append').each('li',function(){
var newLink = $(this).find('div').css('background-image').match(/^(.*?\.jpg)/);
$(this).find('div').css('background-image',newLink);
})
But this is still buggy so if someone as a better idea of how shall I do this ?
Here is a JsFiddle if it can provide some help.
Edit + Answer
Ok so as has said #Hitmands just below it was a problem of URI encoding.
So this is the clearest and smartest solution :
$('.imgLittle').each(function() {
newLink = decodeURIComponent($(this).css('background-image'));
$(this).css('background-image',newLink)
})
And here is the final (working) JsFiddle.
Hope this could help someone once.
If you try to exec this window.decodeURIComponent("%3Fv%3D1") in your console it returns a simply "?v=1", this could be a way to manage the client-caching... when the v (version?) param changes it forces the browser to download the resource even is already cached...
I think that this is an important information that you cannot strip!
A smart solution could be a simply decoding of this param.... you should, but I'm not sure, try http://www.smarty.net/docs/en/language.modifier.unescape.tpl...
If isn't possible to decode the URI Component via smarty, you can do it via Javascript using the code that you posted below with the window.decodeURIComponent instead of regex-stripping.

google custom search engine multiple parameters query

Sry, for unclear title to the question, I hope someone will correct it to something more precise.
The situation:
I am using a google custom search engine which is set to search only two sites and display results only.In control panel of GSE when I go to advanced tab in search features, i can specify query parameter that it will look for. after I am done i get a javascript code to paste in my page's code.
I believe that this javascript code listens to $_GET['q'] variable and then produce results based on that.
Problem :
I have four field in a page and a php form set up to display result from the other sites, the end result is the URL with four get variables like $_GET['q1'] to $_GET['q4']. I don't have a $_GET['q'] in the page or anywhere else.
What I tried:
I just can't make the earlier form to print another or a combined get parameter as $_GET['q']. i'll have to add a lot of JS and a hidden input field i guess.
I tried setting $_GET['q'] in php as $_GET['q']=$_GET[q1]; and then adding the javascript for custom search engine.
I tried adding a link to update or set GET anything but this doesn't seem to work.
I think the reason is maybe lack of knowledge about how GSE gets a keyword,search and returns result, and lack of knowledge about $_GET parameter.
Question:
Anyway I can set $_GET['q'] in my php script(dynamically?) that google custom search engine javascript will understand and work fluently ??
Is there any other better way to do this.
This doesn't need me to show code but here is some code that may be relevant to the question :
GOOGLE CUSTOM JAVASCRIPT
<script>
(function() {
var cx = '010474608308939830468:6s3kklfffuu';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchresults-only></gcse:searchresults-only>
And here is my php approach :
$_GET['q'] = $_GET['q1'] ;
any help will be greatly appreciated and hope you can also clear out on the functioning of GSE for fellow readers. Thanks, in advance
So I was unclear from your question if you need to search ALL of the queries, or if the user is only going to fill in one of the forms. If the user fills in only one form, this should work - using PHP to check which query variable is used, and then having the CSE look at that parameter.
<?php
if (isset($_GET['q1']) && $_GET['q1'])
{$param = 'q1';
} elseif (isset($_GET['q2'])) {
$param = 'q2';
}
echo "<gcse:search queryParameterName='".$param."'></gcse:search>";
?>
If you need to concatenate all your queries into one, your best bet would probably be to rewrite the URL using htaccess before it hits the page, so that all the parameters are concatenated into one query parameter before the page loads. Then set the queryParameterName to whatever you have called that variable.
If you don't have access to creating a rewrite rule on your server, you could explore this answer: changing the query string without reloading the page. Just do that ahead of the tag, which evaluates the query string to figure out what you are searching for.

Redirect URL paths included with Javascript

I have seen several questions about redirecting to a page via javascript, and that's great, but all of them just redirect to a base url. I want to include a variable path. So say for instance I have http://www.glas.com/my/url/path I want to use window.location.replace() to replace the glas part only, and change it to glass. I'm a beginner at JavaScript so any help would be much appreciated.
This is what I have tried so far:
var path = ''; // this is where I'm stuck
window.location.replace("http://www.glass.com/" + path);
Where the path variable would parse the current URL for anything after the /. I have tried looking at JavaScript's match and regexp, but it all seems very confusing.
Try
window.location = window.location.href.replace('glas', 'glass');
You would do something like:
window.location.href = window.location.href.replace('glas','glass');

Prepend all links that are not intra-site with a string in JS... Can you just proofread this quick little script I hacked together?

The idea is to route the outgoing links through DDG's redirect service, to conceal my site in referrers. Is there anything wrong with the script as it is?
Also, see the comment in the code... Is the mentioned alternative superior? Inferior? Identical?
Thanks again!
var baseUrl='https://duckduckgo.com/l/?u=';
var invisibleHost=window.location.hostname;
var pageLinks=document.getElementsByTagName('a');
var n_links=pageLinks.length;
var leaveAsIs=true;
for(var i=0;i<n_links;i++){
var presentLink=pageLinks[i].href;
leaveAsIs=true;
if(/^https?:\/\//i.test(presentLink) && invisibleHost.test(presentLink))
leaveAsIs=false;
pageLinks[i].href=leaveAsIs?presentLink:baseUrl+presentLink;
}
always use test() unless you need to return, rather than simply test for, a match.
Also, your nested if conditions could be one, multi-condition, and you don't need two REGEXs for http and https - just use one but stipulate that the 's' is optional:
/^https?:\/\//i.test(presentLink)

Categories

Resources