How to open an https website on a button click javascript? - javascript

The piece of code I am using is
window.location.href = "https://www.google.com/";
But i stays on the same page,doesn't go to google.com.I was able to get this work with an http page.
window.location.href = "http://www.tutorialspoint.com/mongodb/mongodb_insert_document.htm";
This is the complete code for the same
<form name="form1" onsubmit="submis()" action="/post.php" method="POST"
and the function submis()
function submis()
{
window.location.href = "https://www.google.com/";
}
and post.php
if (!empty($_POST))
{
//saving the contents of the post request to a file.
}

<script>
function goto(){
window.location.href = "https://www.google.com/";
}
</script>
in html code go to google
it works

I was able to make this work by changing the code to,
window.top.location.href="https://www.google.com"

Related

Get current page URL in href

Currently working on a share button for Vk however having a dynamic site, I need the button to automatically use the correct page URL.
This is the code snippet they offer for a static site, how could I adjust this for multiple pages ?
<a href="http://vk.com/share.php?url=http://example.com" target="_blank">
Easiest way is by JavaScript:
document.getElementById("vkShare").href = 'http://vk.com/share.php?url='+document.location.href;
<a id="vkShare" target="_blank">Share to VK</a>
You can get the current page's URL by using a window object like this: window.location.href.
simple
works on all sites
function share(){
function copyToClipboard(text) {
window.prompt('share us',text);
}
copyToClipboard(window.location.href);
}
<button onclick="javascript:share();">share </button>
Using php to get the url, try to use $_SERVER['HTTP_HOST'] to get the host and $_SERVER['REQUEST_URI'] to get any query string attached to the url.
<?php
$uri = $_SERVER['REQUEST_URI'];
$host=$_SERVER['HTTP_HOST'];
//$complete_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//To use whether HTTP OR HTTPS
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === "on"){
$htp = "https";
}else{
$htp = "http";
}
$complete_url = $htp."//:".$host.$uri;
?>
<a href="<?=$complete_url?>" target="_blank">

how to redirect old tab after I submit to a new tab

I´m submitting a form in a new tab with:
<form action="xxx.php" target="_blank" method="post">
But the old tab stays in the form and I´d like to send it to another page after submit.
Thanks!
Two Options :
1) Just remove the target="_blank" and redirect the form in the same page.
2) Use Jquery to perform a function after form is submitted :
$('#myForm').submit(function(e) {
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
});
Made it!..
through GET
function load(){
<?php
$url = "XXX.php?VAR=".$Z;
echo "window.open('$url');".PHP_EOL;
echo "window.location.href = 'YYY.php';";
?>
}

Link to a page and trigger ajax via the same button

I'm trying to make a div button which triggers ajax and links to a page which the ajax is posting data to. I have the ajax working on click, but can't seem to get the link to work at all with it, all the ways I've tried to get the link working do nothing.
I have a div button:
<div class="cman shadow1" id="search_button" value="carSearch"/>
Start Search
</div>
Which triggers:
function carSearch()
{
$.ajax({
type: "POST",
url: 'searchpost.php',
data:
{
mpg : $('.mpg').val()
},
success: function(data)
{
alert("success! "+$('.mpg').val());
}
});
}
Edit:
Feel I should mention that the page I wish to link to is atm:
<?php
session_start();
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('cdb', $conn);
if(isset($_POST['mpg']))
{
$mpg = (int)($_POST["mpg"]);
header("Location: home.php");
exit;
}
?>
I wish to use this to gather all of the search fields the user enters to then load the search results page. The above code for it is just for testing right now.
I mention this as user #Doge suggested doing document.location.href = "searchpost.php" in the success handler of the ajax, this did cause the page to change but the page didn't run the isset and the redirect, so the js variable wasn't posted, or it was but this instance of the page didn't get it.
The isset and redirect do work as using the XHR Network tab on my page the page it is to redirect to appears.
If you must do this via js then create a hidden form, populate it and submit it:
html:
<form method="post" id="hiddenform" action="searchpost.php">
<input type="hidden" name="mpg" id="mpg"/>
</form>
js:
function carSearch()
{
$('#mpg').val(yourjsvariable);
$('#hiddenform').submit();
}

Replacing GET with POST

Here I am trying to redirect new page and I want to send a variable. It uses GET.
How can I do it with POST?
window.location.href = 'start.php?userid='+userid;
start.php
<?php
$user_id=$_GET['userid']; //should be post
?>
You will have to submit the data to the server, not just redirect the browser.
In your case, as it looks like you want full page refresh anyway just create a form on the fly:
var oForm = document.createElement("form");
oForm.method = "POST";
oForm.action = "start.php";
var oInput = document.createElement("input");
oInput.name = "userid";
oInput.value = userid;
oForm.appendChild(oInput);
document.body.appendChild(oForm);
oForm.submit();
You need to declare a fake form in HTML and a link that will trigger javascript to submit the form, like below
<form action="start.php" method="post">
<input type="hidden" name="userid" value="[userid]">
</form>
<script type="text/javascript">
function submitlink()
{
document.forms[0].submit();
}
</script>
<a class="buttondown" onclick="submitlink()">Submit Link</a>
I think this answer may help you:
How do you force a web browser to use POST when getting a url?
You just need to create a form on demand using javascript to send data with POST method when clicking a link.
So basically it's just this part:
<script type="text/javascript">
function submitAsPost(url) {
var postForm = document.createElement('form');
postForm.action = url;
postForm.method = 'post';
var bodyTag = document.getElementsByTagName('body')[0];
bodyTag.appendChild(postForm);
postForm.submit();
}
</script>
this is my post link
I was in the same problem you had. If you are using jquery.redirect.min.js as your jquery plugin, You can use as below. It gives you POST method.
$().redirect("myPhp.php", { name: "John" });
All you need is to download jquery.redirect.min.js file from here and link it with your php file, and use as above. That's it. Hope I helped.
Works fine for me.
You can post form by html tag and . Otherwise, see "jQuery.post" for async post.
For the 1st case you create FORM tag, put INPUT type="hidden" inside and set its value that will be posted.

Javascript - getElementById.innerHTML Problem

I want to create a simple formular which leads the user to 2 different pages (depending on what they choose). I'am using getElementById.innerHTML to differ the code within the site to create the two different <form action="SENDTHEUSERHERE" method="POST">-HTML-Elements.
Here's the code:
if (blabla which works)
{
document.getElementById("insert").innerHTML = "<form action='insert.php' method='POST'>";
}
else if (blabla which works)
{
document.getElementById("service").innerHTML = "<form action='service.php' method='POST'>";
}
// MORE BLABLA-CODE
<span id='insert'></span>
<span id='service'></span>
// REST OF THE FORMULAR
When I'am sending this formular, it leads me simply to the same page it was sent from. I guess that means, that those .innerHTML-Elements aren't recognized. Have you any suggestions how I can solve this? Btw.. yes, I'am a Javascript Noob -_-
Thanks for any help!
Just change the form action instead of using innerHTML...
if (conditionOne) {
document.forms['formName'].action = 'insert.php';
} else if (conditionTwo) {
document.forms['formName'].action = 'service.php';
} else {
alert('Error!');
}
You're going to have to post either your full HTML page + javascript code or a working example showing the problem. From the little you posted, it's impossible to tell what could be wrong.
But, that said, you're going about this problem in the wrong way. Instead of changing the innerHTML of some nodes, have one form with no action, and then in javascript do something like:
<form id="myForm">
var myForm = document.getElementById( "myForm" );
myForm.action = ( someConditionHere ) ? "insert.php" : "service.php";
If your example is what you need to do, you can just modify the action attribute of an existing form.
<form method='post' action='nowhere'>
<stuff>
</form>
<script type='text/javascript'>
if (something) {
document.forms[0].action = 'insert.php';
} else {
//etc
}
</script>

Categories