Okay, so I have a Google Site that I need to redirect to a website.
So for example, I have sites.google.com/xxx/xxx
And when people enter that, I want it to redirect to my website
www.xxxxxxxx.com
How do I do this?
You could bypass this by using js to change href of some hidden element and than use javascript to "click" on this element.
I've used this to create contacts search element directly on sites.
Markup
<button onclick="functionSearch()" id="buttonSearch">
<input type="text" id="inputSearch" value="" placeholder="Search">
Javascript
function functionSearch() {
var inputSearch = document.getElementById("inputSearch");
if (inputSearch.value != "") {
document.getElementById("searchHelper").href = "https://contacts.google.com/search/" + inputSearch.value;
document.getElementById("searchHelper").click();
}
}
On App Script it's not possible.
In App Script , you can do this:
var anchor = app.createAnchor("link", "http://www.google.com");
Look the doc :
=> https://developers.google.com/apps-script/reference/ui/anchor
On javascript :
// click on a link
window.location.href = "http://google.com";
// redirect
window.location.replace("http://google.com");
Related
I the below script, once the button is pressed, a new browser tab with Google is opened. I then want to fill the search box on this newly opened tab.
How do I switch focus to this new tab, so that I can wait for it to load completely and then fill the search box?
<button id="start">Google</button>
<script>
function open_google_in_new_tab() {
var url = "https://www.google.com";
window.open(url);
window.onload(fill_search_box()); // here it will try to operate on the old tab
};
function fill_search_box() {
var search_box = document.getElementsByName("q")[0];
search_box.value = "Some text";
};
var start_button = document.getElementById("start");
start_button.addEventListener("click", open_google_in_new_tab);
</script>
You can't exactly target another element in another document from your script, however, you can set the textbox value of the Google textbox using a query string:
www.google.com?q=SearchTerm
When creating the SearchTerm you should also use encodeURI to ensure that your attached search query meets URL standards
Also, if you wish to open the window in another tag you can use window.open(url, "_blank") to open.
See example below:
Note:- The page will not open due to snippet restrictions - so run in your own browser.
<button id="start">Google</button>
<script>
function open_google_in_new_tab() {
var text = "Some text";
var url = "https://www.google.com?q=" + encodeURI(text);
console.log(url)
window.open(url, '_blank');
};
var start_button = document.getElementById("start");
start_button.addEventListener("click", open_google_in_new_tab);
</script>
You cannot do that with Javascript unfortunately.
However, this might make you achieve something similar if you are willing to try.
You can manipulate the search string via parameter named "q" in the query string of the Google website. So, in order to make the user to go the Google and search this particular word, you attach the keyword to it. Something like this:
var url = "https://www.google.com?q=searchKeyword";
Following is the code I have got for location redirect to SMS app when the user is on a mobile browser-
window.onload = function() {
window.location ="sms:12345?body=" + encodeURIComponent("TEST");
}
This code works perfectly on JS fiddle when running on a mobile
JS Fiddle link -https://jsfiddle.net/netstarter/rwqyp2tn/1/
Easiest and appropriate way of doing this would be creating an hidden link and triggering it directly.
window.onload = () => {
let element = document.getElementById("hiddenAppLink");
element && element.click();
};
<!DOCTYPE html>
<html>
<body>
</body>
</html>
You can also trigger it based on a condition by tracking the state if its closed or not in a variable instead bugging the user on every load (You can also use localStorage can't do it in fiddle => security violation). something like this.
window.onload = () => {
if(window.hideDialouge != true){
let element = document.getElementById("hiddenAppLink");
element && element.click();
//Track if its alredy shown
window.hideDialouge = true;
}
};
<html>
<body>
</body>
</html>
SMS URL seems to be only working when you are about to open new SMS URL. It fails to work when you manually enter the URL or use window.location redirect.
You can use two methods to achieve this:
Open hidden link.
Use window.open (This might ask user to allow popups on your page)
1. Open hidden link.
Create a hidden link and open it.
window.onload = function() {
let elem = document.getElementById("loadSMS");
elem && elem.click();
}
<a href="sms:1-111-1111?body=Blah" style="display:none" id="loadSMS">
</a>
2. Use window.open instead of changing window.location
window.onload = function() {
window.open("sms:1-111-1111?body=Blah");
}
I am using the Ionic framework.
I have a form as such:
<form action='https://secure.payu.in/_payment' method="POST" target="...">
......
</form>
Upon form submission, I need the result to open in an in-app browser.
As a dirty hack, I pointed the form submission target as an iframe in the current tab, but I want to avoid this.
Ideally, I need the form action url to open in an in-app browser (and not replace the app in the webview which would result in my loosing control of the app).
I tried to give "_blank" in the target but it doesn't seem to work on the device.
I also know the conventional way to open an in-app browser would be to do something like this:
var ref = window.open("http://example.com", "_blank", "location=no");
However, what I need is upon the form submission, an in-app browser should open and not triggering an in-app browser via js.
Any help appreciated!
I faced the same problem recently. I resolved this by injecting a form with the executeScript method.
First, you have to open a transition page (a blank page or other but with jquery referenced, you have to host it somewhere) in the appbrowser window:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
Then, add a loadstop listener and call executeScript form there (when the URL matches the transition page's URL).
var browserWindow = window.open('http://transitionpage.com', '_blank', 'location=yes,hidden=yes,hardwareback=no');
var transitionLoaded = false;
var finalURL = 'https://finalurl.com';
var paramName = 'SOME_PARAM';
var paramValue = 'asdf';
function injectForm() {
browserWindow.executeScript({
code: "var $form;$form = $(document.createElement('form')).css({ display: 'none'}).attr('method', 'POST').attr('action', '" + finalURL + "');$('body').append($form);var $input = $(document.createElement('input')).attr('name', '" + paramName + "').val('" + paramValue + "');$form.append($input);$form.submit();"
}, function() {
//Form injected
transitionLoaded = true;
});
}
browserWindow.addEventListener('loadstop', function(event) {
if (event.url.indexOf('http://transitionpage.com') !== -1 && !transitionLoaded) {
injectForm();
}
});
browserWindow.show();
I have page the has some data in tabs, Im trying to write a function so that when links are click from another page can load the page with the tabs on and show the correct tab. This is working with the code below, minus the actual changing tabs function. But for some reason using the window.location..... as a variable still scroll the page down to the matching id. Is there another way to get the string in the url after the #. Or can i do it this way but not have to jump to the id? thanks
function loadTab(){
var linkToTab = window.location.hash.substr(1);
var linkClass = '.'+linkToTab
if(window.location.hash != '') {
changeTabs(linkClass);
}else{
$('.companyLink:first').addClass('active');
$('.companyBio:first').addClass('active');
$('.companyBio:first').fadeIn();
};
}
The hash character is for anchors.
Use a question mark instead of a hash.
<a href="index.html?tabname">
and
var linkToTab = window.location.search.substr(1);
var linkClass = '.'+linkToTab
if(window.location.search != '') {
The # part of the URL is traditionally used for anchors. The 'jumping' you see is the original feature.
Modern websites and web-applications use it build a history as HTML5's history feature isn't widely supported yet.
To avoid the jumping add event.preventDefault to your links, like:
Tab1
<script type="text/javascript">
document.getElementById("tab1handle").onclick = function (event) {
event.preventDefault();
}
</script>
You can also make sure the anchor is not defined. In this case the browser will jump to the top of the page. It's up to you whether this is undesirable or not.
I need to automatically open a user's default email client when they save some content on a page. I need to populate the email subject, to address, and put some content in the email body.
What is the best option to achieve this?
I'm aware of the mailto: attribute, but the user must click on this and I'm not sure it allows you to specifiy the subject and content?
As described by RFC 6068, mailto allows you to specify subject and body, as well as cc fields. For example:
mailto:username#example.com?subject=Subject&body=message%20goes%20here
User doesn't need to click a link if you force it to be opened with JavaScript
window.location.href = "mailto:user#example.com?subject=Subject&body=message%20goes%20here";
Be aware that there is no single, standard way in which browsers/email clients handle mailto links (e.g. subject and body fields may be discarded without a warning). Also there is a risk that popup and ad blockers, anti-virus software etc. may silently block forced opening of mailto links.
JQuery:
$(function () {
$('.SendEmail').click(function (event) {
var email = 'sample#gmail.com';
var subject = 'Test';
var emailBody = 'Hi Sample,';
var attach = 'path';
document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody+
"?attach="+attach;
});
});
HTML:
<button class="SendEmail">Send Email</button>
Implemented this way without using Jquery:
<button class="emailReplyButton" onClick="sendEmail(message)">Reply</button>
sendEmail(message) {
var email = message.emailId;
var subject = message.subject;
var emailBody = 'Hi '+message.from;
document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody;
}
Try this:
It will open the default mail directly.
<img src="ICON2.png">