JavaScript Redirect Issue - javascript

I'm building a cross-platform app with JavaScript. I want to redirect to a different page (in a different domain) but it is not working. I tried:
<html>
<head>
<script>
function redir() {
window.location.href = "http://www.example.com";
}
</script>
</head>
<body onload="redir()">
</body>
</html>
It gived me a blank page. I tried with a different code, I tried iframe like this:
<html>
<head>
</head>
<body>
<iframe src="http://www.example.com" />
</body>
</html>
But it didn't worked again. It gave me a blank iframe. Can someone help me?

function Redirect() {
window.location="https://www.tutorialspoint.com";
}
<form>
<input type="button" value="Redirect Me" onclick="Redirect();" />
</form>

<body onload="redir()"></body>

Related

How to call grab the value of a button inside the iframe using jquery?

I have 2 html pages (a.html and b.html). I have made an <iframe> and set b.html as a source of a.html. But how do I call at a.html to grab the value of b.html button using jquery via class name? FYI: Both are in the same domain. How can I achieve that?
a.html
<html>
<head>
<title>a.html</title>
<script>
function howToGetValueOfBtn() {
}
</script>
</head>
<body>
<iframe src="b.html"></iframe>
</body>
</html>
b.html
<html>
<body>
<input type="button" value="getThisValue" class="btn"/>
</body>
</html>
Thanks for all the helps in advance!
Try this:
var button_value = $('iframe').contents().find('.btn').val();
But it would be best if you give your button an Id
var button_value = $('iframe').contents().find('#btn_Value').val();
<input type="button" value="getThisValue" class="btn" id="btn_Value"/>
Hope this helps

How to set real time input value to link through iFrame?

Please see below 2 HTML pages, I have 1 page inside iFrame call other page.
what I exactly need, I want in "iframe.html" you can see text field. when I write something in that input box, real time that value should take inside href=""
Note : iframe.html page is pure html page. I can't use jquery inside that page. I have to access that page from index.html page.
I have tried some jquery code, but only I wrote function.
this is index.html page.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<iframe src="iframe.html" id="myframe"></iframe>
<script type="text/javascript">
$($("#myframe").contents().find('body')).on("click", 'a[href="#editable-link"]', function(e) {
});
</script>
</body>
</html>
And this is "iframe.html" page.
<html>
<head>
<input type="text" placeholder="Enter URL">
Read More
</body>
</html>
can anyone help me to resolve this problem. it will very helpful.
thanks in advance.
Try
$('#myframe').load(function(){
$('#myframe').contents().find('input').bind('input',function(e) {
var url = $('#myframe').contents().find('input').val();
$('#myframe').contents().find('#editable-link').prop('href',url);
});
});

window.parent.document working in firefox , but not in chrome and IE

My concept is to update the value of the text box in the main page from the iframe . This code is working in firefox , but not working in Internet Explorer and Chrome . Both main.html and frame.html are in same location . I need suggestions to make it work in all the browsers .
main.html
<!DOCTYPE html>
<html>
<head>
<title> main window </title>
</head>
<body>
Parent textbox :<input type="text" id="parentbox"></br></br></br>
<iframe src="frame.html" ></iframe>
</body>
</html>
frame.html
<!DOCTYPE html>
<html>
<head>
<title> frame window </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function PushValues()
{
var frame_value = document.getElementById("framebox").value;
window.parent.document.getElementById("parentbox").value =frame_value;
}
</script>
</head>
<body>
<input type="text" id="framebox" >
<input type="button" value ="fill" onclick="PushValues()">
</body>
</html>
As per security policies, cross-domain access is restricted. This will happen if you are trying to show a page from domain 1 in domain 2 and try to manipulate the DOM of page in domain 2 from the script in domain 1. If you are running the pages from same location on a server. This shouldn't happen. However, if you are just saving them as HTML files and trying to open them in your browser, it should not work. I have created two jsbins for your code and it is working on chrome. Try to access them using the below links.
Main.html: http://jsbin.com/afazEDE/1
iframe.html: http://jsbin.com/ayacEXa/1/
Try to run main.html in edit mode in JSBin by keeping console open in chrome (F12) and click fill button. It will not work and will show you the error. If you run them as it is (in run mode of JSBin), it will work.
Jquery -
function PushValues()
{
var frame_value = $('#framebox').val();
parent.$('body').find('#parentbox').val(frame_value);
}
It's always work for me.
Run this code on a server like xamp or wamp it wont work directly
Main.html
<!DOCTYPE html>
<html>
<head>
<title> main window </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
Parent textbox :<input type="text" id="parentbox" value=""></br></br></br>
<iframe src="iframe.html"></iframe>
<script>
window._fn = {
printval: function (response) {
$("input").val(response);
},
};
</script>
</body>
iframe
<!DOCTYPE html>
<html>
<head>
<title> frame window </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<input type="text" id="framebox">
<input type="button" value="fill" onclick="PushValues()">
<script language="javascript">
function PushValues() {
window.parent._fn['printval']($('input').val());
}
</script>
</body>
</html>
Since you're using jQuery try this
var frame_value = $('#framebox').val();
$('#parentbox', window.parent.document).val(frame_value);
You should try P3P policy which is highly related to iframes and Internet Explorer.
response header set to the iframe document
header key= 'P3P' header value: 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'

open page automatically using javascript

Essentially, all I want to do is open an external web page after the current page is loaded via java script.
open my page -> javascript tells browser to open external page -> external page being loaded into the broser
How may I accomplish this?
you may use this
<html>
<head>
<script type="text/javascript">
function load()
{
window.location.href = "http://externalpage.com";
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function load()
{
window.location.href = "http://externalpage.com";
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
Hope it should be window.location. Check the code.
Technically you can:
location.href = "http://example.net/";
… but you should perform an HTTP redirect instead as that is more reliable, faster and better food for search engines.
You can also use the "open" method to open the source file or url on a new window.
window.open("anyfile.*");
or
window.open("http://anylocation.com");
Hi please try this code for page loading time we will redirect whatever u configured the urls.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
window.open('https://google.com');
}
</script>
</head>
<body onload="myFunction()">
</body>
</html>
<body>
<script>
document.body.innerHTML += 'Link';
document.getElementById("link").click();
</script>
<body>

iframe across domain get value

How can I get the some value from other domain page?
for example
two page from different domain
test.html:
code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<span id="data"></span>
<iframe name="dd" src="http://otherdomain.com/innerpage.html" style="width:600px;height:500px;"></iframe>
</div>
</body>
</html>
innerpage.html(on another domain)
code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function SendDataToParent(){
var dataId = parent.document.getElementById("data"),
data = document.getElementById("iframeData").value;
dataId.innerHTML ="<input type='hidden' value='"+data+"' name='dataFromChildIframe'/>";
}
</script>
</head>
<body>
<div>
<button onclick="SendDataToParent();">SendDataToParent</button>
<input type="text" id ="iframeData" value="some content here">
</div>
</body>
</html>
I want to get input with the id of iframeData value ,and send this value to the parent page
but code don't work ,How to do this?
For security reasons, it is completely impossible to pages in two different domains to communicate on the client in current browsers.
As a workaround, you can use JSONP in both pages.

Categories

Resources