the community!
I'm going through some issues. the redirection feature doesn't work well when I try to post the link on the Facebook platform. in general, the redirection only works outside facebook community. when I click on the shared link on Facebook automatically set this parameter at the end of URL ?fbclid=
example : https://myblog.blogspot.com/go/fb?fbclid=
but when I put the link directly on the browser works properly
here is my code
<script language='javascript'>
//<![CDATA[
var key = window.location.href.split("go/")[1].replace("/","")
var urls={
'fb':'https://www.facebook.com',
}
if(key){
if(urls[key]){
window.location.href=urls[key]
}else{
document.write("'"+key+"' not found 😞");
}
}
//]]>
</script>
Use window.location.pathname instead of window.location.href.
<script>
//<![CDATA[
var key = window.location.pathname.split("go/")[1].replace("/","")
var urls={
'fb':'https://www.facebook.com',
}
if(key){
if(urls[key]){
window.location.href=urls[key]
} else {
document.write("'"+key+"' not found 😞");
}
}
//]]>
</script>
Related
I know this has been covered extensively as separate issues; I've tried copying verified answers, but I'm having trouble homologating Javascript conditional statements and a link URL change.
Essentially, what I need to do is detect mobile users and change a conference call URL to a tel link. I've been using if (screen.width <=699) { as the condition and it works on redirects. Here's what I've tried:
<script type="text/javascript">
<!--
var call=document.getElementByID('phone');
if (screen.width <= 699) {
call.write('<a href="tel:!PHONENUMBER!">');
else
call.write('<a href="!URL!" target="blank">);
}
//--!>
</script>
</head><body>
...
...
I've also tried these with corresponding else statements to no avail:
no var and document.getElementByID('phone').write
onclick = function() { window.location.href ='tel:!PHONENUMBER!};.
call.src.replace('tel:!PHONENUMBER!');
call.setAttribute("href",'tel:!PHONENUMBER!');
I apologize if this is a super basic issue - I'm still learning Javascript. Thank you in advance.
Will
You need to either wait for the page to finish loading before you execute your JavaScript or move your script block to the bottom of the HTML.
If you want to wait for the page to finish loading, then wrap your code in the following:
window.onload = function () {
// your code goes here
}
Figured it out with help from #sepbot.
<body onload="mobileURL()">
<div id="phone"></div>
<script type="text/javascript">
function mobileURL() {
var a = document.createElement("a");
a.innerHTML="!LINKTEXT!";
var myPhone = document.getElementById('phone');
if (screen.width <= 699) {
a.setAttribute('href', '!TELEPHONE!');
myPhone.appendChild(a);
} else {
a.setAttribute('href', '!URL!');
myPhone.appendChild(a);
}
}
</script>
I am trying to copy 'window.location.href' e.g. the URL of the current page to clipboard from my extension.
My issue is that when I copy the URL to clipboard, it is the extensions URL that is copied and not the page I am visiting.
Extensionbar:
<!DOCTYPE HTML>
<html>
<head>
<button onclick="copyFunction();">Copy</button>
<script type="text/javascript">
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}
</script>
</head>
</html>
From my understanding the solution should be this, but I fear I am too clueless how to proceed: https://developer.apple.com/documentation/safariservices/safari_app_extensions/passing_messages_between_safari_app_extensions_and_injected_scripts
This is how I (tried to) proceed, by creating a global.html page and an injected script.
Global page:
<!DOCTYPE HTML>
<script>
safari.application.addEventListener("command", copyFunction, false);
function copyFunctionEvent(event) {
if (event.command == "CopyToClipboard") {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("CopyToClipboard", "all");
}
}
</script>
Injected script:
function myextension_openAll(event){
if (event.name == 'CopyToClipboard'){
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}
}
safari.self.addEventListener("message", myextension_openAll, true);
Actual:
safari-extension://com.myextension-0000000000/abc123/extensionbar.html
Expected:
http://www.google.com (e.g. if current tab)
From your code above (Extensionbar html), you seem to write legacy Safari extension (.safariextz), and it has been deprecated. See What’s New in Safari and WebKit" session on WWDC18
I recommend you rewrite your code into Safari App Extension by following process, which can be written in Swift. I'm not sure why wrong URL is copied to clipboard in your code, but rewriting your code would solve the problem as a result.
Creating App Extension project
Create App Extension by following [File] -> [New] -> [Project...] then choose [Safari Extension App] on Xcode. Project template contains example of menubar implementation.
Copying location.href by clicking menu bar button
Following code would add functionality to copy location.href when you click menu bar button.
Just paste this into SafariExtensionHandler.swift.
class SafariExtensionHandler: SFSafariExtensionHandler {
override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) {
// WHen injected script calls safari.extension.dispatchMessage, the message will come here
guard let href = userInfo?["href"] as? String else { return }
// Save href to clipboard
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(href, forType: .string)
}
override func toolbarItemClicked(in window: SFSafariWindow) {
// Request injected script a message to send location.href
window.getActiveTab { currentTab in
currentTab!.getActivePage { currentPage in
currentPage!.dispatchMessageToScript(withName: "getHref", userInfo: nil)
}
}
}
}
And injected script (script.js) as follows.
safari.self.addEventListener("message", function(event) {
console.log("event received");
safari.extension.dispatchMessage("sendHref", { "href": location.href });
});
Working Example
Complete working code here, This may help your work. Good luck :)
https://github.com/horimislime/safari-extension-menubar-example
I have this redirect, and it works great. However, I want the user to be able to access the desktop site when they request it via a link on the external mobile site at m.flyeurova.com. How can I do this?
<script type="text/javascript">
var isMobile = function() {
//console.log("Navigator: " + navigator.userAgent);
return /(iphone|ipod|ipad|android|blackberry|windows ce|palm|symbian)/i.test(navigator.userAgent);
};
if(isMobile()) {
window.location.href = "http://m.flyeurova.com/";
}
</script>
I have the below script on a page, let's say "child.html".
I only want the browser to see the function opener.opener = opener; opener.close() if the referring URL is the one listed in the if statement of the script (parent.html), otherwise I don't want the browser to see it.
The page that this script is on is a child page (a popup) of a parent page. Users (hopefully) will only access the parent page, which will subsequently open this child page and close the parent page at the same time using the opener.opener = opener; opener.close() function.
The function works fine and does what I need it to do (closes the parent after opening the popup). But, if the child page URL is accessed directly (in other words without being directed from the parent page), then obviously there is no parent page to close and I get the script error:
'opener' is null or not an object
I understand the error message, hence why I'm trying to hide 'opener' if the referring URL IS NOT parent.html so that if someone happens to access the child page directly, opener is hidden and no error message.
What do I need to change in the below script to make this work?
<script type="text/javascript">
//<![CDATA[<!--
if (document.referrer != '../parent.html') {
opener.opener = opener;
opener.close();}
else {}
//-->
//]]>
</script>
Alternatively, I tried writing the following:
<script type="text/javascript">
//<![CDATA[<!--
if (document.referrer = '../parent.html') {
return false;
} else {
opener.opener = opener;
opener.close();}
//-->
//]]>
</script>
But that returns the script error: 'return' statement outside of function.
Thanks very much in advance for any help you can offer!
Your first script is correct if you put an ANDcondition with the initialization of opener variable like:
if (document.referrer !== '../popup.html' && opener) { ... }
For the second one you have to write down a function if you want to use the return statement like:
<script type="text/javascript">
//<![CDATA[<!--
function(){
function myFunct() {
if (document.referrer === '../parent.html' || !opener) {
return false;
} else {
opener.opener = opener;
opener.close();
return true;
}
}
myFunct();
}();​
//-->
//]]>
</script>
Thanks to Dario for the suggestion :) I rewrote it and the below script works:
<script type="text/javascript">
//<![CDATA[<!--
if (document.referrer != '../popup.html' && opener) {
opener.opener = opener;
opener.close();}
//-->
//]]>
</script>
i am new to javascript and i do not know how to do a hash but here is my code the code trigers curency to change on my website currently only byt clicking on a flag but i would like to use a hash like http://hostchick.co.uk/#ca to change the princing to canadian
<script type="text/javascript" language="javascript">
window.location.hash = "#ca"
{
function changeText(1);
function changeText(2)
}
</script>
<script language="javascript">
function changeText(idElement){
if(idElement==1){
document.getElementById('element'+1).innerHTML ='£1.99';
} else if(idElement==2){
document.getElementById('element'+2).innerHTML ='£2.99';
}
if(idElement==3){
document.getElementById('element'+1).innerHTML ='$3.15';
} else if(idElement==4){
document.getElementById('element'+2).innerHTML ='$4.73';
}
if(idElement==33){
document.getElementById('element'+1).innerHTML ='$3.16';
} else if(idElement==44){
document.getElementById('element'+2).innerHTML ='$4.75';
}
if(idElement==333){
document.getElementById('element'+1).innerHTML ='Â¥10.01';
} else if(idElement==444){
document.getElementById('element'+2).innerHTML ='Â¥29.94';
}
}
</script>
<li><a href="#" onClick="javascript:changeText(1);javascript:changeText(2)">
test
</a>
</li>
window.location.hash = "#ca"
{
function changeText(1);
function changeText(2)
}
is syntactically invalid JavaScript, so the assignment to location.hash never happens.
I'm not quote clear on what you're trying to do, but maybe
function changeText(1);
function changeText(2)
should be
changeText(1);
changeText(2);
Changing the hash does not cause the page to reload, so the server isn't involved. You can always use JavaScript to examine the hash and behave differently based on it.