ReCAPTCHA ajax loaded theme problem - javascript

Can not figure out how to theme an ajax loaded recaptcha. The below code does not work.
From Google Recaptcha
Saw this post Recaptcha ajax API custom theme not working, but I am definitely viewing in localhost and recaptcha is working fine, just not changing themes.
Anyone have advice on how to get the white theme to work?
<script type='text/javascript'>
var RecaptchaOptions = {
theme : 'white'
};
</script>
<div id="recaptcha_content">
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=6Ldr7woAAAAAADa_69Mr-EZKAeYyEx9N08q" height="300" width="500" frameborder="0"></iframe><br />
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type='hidden' name='recaptcha_response_field' value='manual_challenge' />
</noscript>
</div>
<script type="text/javascript">
$(document).ready(function() {
$.getScript('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js',
function() {Recaptcha.create("6Ldr7woAAAAAADa_69Mr-EZKAeYyEx9N08q", "recaptcha_content");
});
});
</script>

#jhanifen: I got it working after using most of the code from http://wiki.recaptcha.net/index.php/Installation, try this --
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
$.getScript('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js',
function() {
Recaptcha.create("6Ldr7woAAAAAADa_69Mr-EZKAeYyEx9N08q",
"recaptcha_content",
{
theme: "white",
callback: Recaptcha.focus_response_field
}
);
});
});
</script>
<div id="recaptcha_content">
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=6Ldr7woAAAAAADa_69Mr-EZKAeYyEx9N08q" height="300" width="500" frameborder="0"></iframe><br />
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type='hidden' name='recaptcha_response_field' value='manual_challenge' />
</noscript>
</div>
</body>
</html>

I't doesn't look like you are adding your options that you have set RecapthcaOptions. Try changing to:
$.getScript('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js',
function() {Recaptcha.create("6Ldr7woAAAAAADa_69Mr-EZKAeYyEx9N08q", "recaptcha_content", RecaptchaOptions);
});
See the doc's, the third option passed to the .create() method is the options. You are setting up a variable outside the function to set the options, but don't include them.

First of all, your key is invalid or wrong, or you wanted that to be posting here :D
If not wanted. try to get a new one, and maybe make it a global one (could affect the localhost stuff). Or upload and test the code on the domain you specified.
This snippet works for my global public key, i set the theme directly on creation
<html>
<head>
<title>recaptcha test</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
</head>
<body>
<div id="recaptcha_content">
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=publickey" height="300" width="500" frameborder="0"></iframe><br />
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type='hidden' name='recaptcha_response_field' value='manual_challenge' />
</noscript>
</div>
<script type="text/javascript">
$(document).ready(function() {
Recaptcha.create("publickey",
"recaptcha_content", {
theme: "white",
callback: Recaptcha.focus_response_field
});
});
</script>
</body>
</html>

Going the Occam's Razor way, can you verify that the script for setting the theme is before the form element. As stated several times in the docs, the script does not work inside or after the form element.

Related

Change the CSS of a DIV in an iFrame from Parent

I just started to go further in my web development skills in my job and stucked with a little problem.
I have an iFrame (iframe.html), and in this iframe is a div styled as a green square.
I want to change the color of the Div outside the iframe with a button in my Parent (index.html) with an onClick Function.
I tried a few things, like calling a normal Function (document.getElementById and so on). But nothing really worked.
So I thought, that jquery might have some solutions and I'm stucked now in this code, but it doesn't work.
Can anyone help?
PS: this is not a cross-origin case. i got both html files in the same Directory.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/index.css">
<script src="jquery-1.12.2.min.js"></script>
<title>buttonframetest</title>
<script type="text/javascript">
function Clickit() {
$(document).ready(function(){
$('iframe').contents().find('background-color').css('backgroundColor', 'white');
});
}
</script>
</head>
<body>
<iframe id="iframe" src="iframe.html" width="500px;" height="500px">
<p>iFrame nicht darstellbar in deinem Browser</p>
</iframe>
<br>
<button type='button' onClick="Clickit();">Klick</button>
<button type="button" onclick="redy">CHANGE</button>
<br>
<div class="ChangeColor"></div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
This can be done with the DOM iframe object's contentDocument property:
document.frames[0].contentDocument.getElementById('id here').style['background-color']=whatever;
Or, alternatively, with window.postMessage:
// this is in parent
document.frames[0].postMessage('red', '*');
// this is in frame
window.onmessage = function(x) {
if (x.origin == 'http://www.example.com') document.getElementById('id here').style['background-color'] = x.data;
}
Add this,
Update your jQuery CDN
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
function clck(){
$(document).ready(function(){
$('iframe').css({
'background' : '#fff'
});
});
}

JavaScript function called with button onclick, fails with onload

I am trying to use an external JavaScript function in the body tag: <body onload=function()>
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<script type="text/javascript" src="js/mqttws31.js"></script>
<script type="text/javascript" src="js/Long.min.js"></script>
<script type="text/javascript" src="js/ByteBufferAB.min.js"></script>
<script type="text/javascript" src="js/ProtoBuf.min.js"></script>
<script type="text/javascript" src="js/fs.js"></script>
<script type="text/javascript" src="js/bundle.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="cordova_plugins.js"></script>
</head>
<body onload ="heartbeat()">
<h2>A demo for showing the pub/sub between clients and the message broker</h2> <h3>IoTGateway</h3>
Topic :
<input type="text" name="pub"/>
Message:
<input type="text" name="pub"/>
<button onclick="connect()">
Connect
</button>
<br>
<button onclick="publish()">
Publish
</button>
<br>
<h3>BluetoothID</h3> Topic:
<input type="text" name="sub"/>
<button onclick="heartbeat()">
Subscribe
</button>
<br>
<p id = "test"></p>
<p id="sub"></p>
<script>
function heartbeat() {
alert("hello");
try{
MqttPlugin.subscribe({topic: "$EDC/plugin"});
}
catch(err){
alert(err.message);
}
}
/* try{
MqttPlugin.heartbeat({topic: "$EDC/tum/B8:27:EB:A6:A9:8A/HEARTBEAT-V1/mqtt/heartbeat"});
}
catch(err){
alert(err.message);
}*/
function publish() {
MqttPlugin.publish({
topic:"$EDC/plugin",
data:"Mqtt data"
});
}
function subscribe() {
MqttPlugin.subscribe({topic: "$EDC/plugin"});
}
</script>
</body>
</html>
The function heartbeat is called when I call with the event onclick but fails with onload. In case of onload, it throws an error MqttPlugin not defined. Is it because by the time it calls the function, the js files are not loaded? Could someone help me fix this?
I believe that your basic aim here is to subscribe to a topic automatically when the page is loaded. You can either try:
</script>
<style onload="heartbeat()"></style>
</body>
Or try changing the order of imports.
The first suggestion is just an hack not a full proof solution though.
The basic problem is that the heartbeat function is getting called before cordova_plugins.js is getting loaded. So either delaying the function call or loading the file early will do the trick.
Edited:
This jQuery method might just be the solution:
$( window ).load()
Please find documentation here.

How to append a <script></script> along with other data returned from another page when using Jquery ajax?

I'm loading a reCaptcha using ajax. One of the lines in there from what gets returned from the other pages is the line below, and that line wont get loaded into my div when I use $(.cap).html(callBack);
This is the line
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LcXq-oSAAAAAOgwSIew_xq6xkv1QUxrZbRcMz6n"></script>
Is this something that jQuery disallows? Or am I doing something wrong? How can I solve this? How can I get jquery returned from another page, into my <div>?
What I'm testing with, is simple:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>No Tile</title>
<script type="text/javascript" src="http://localhost/site/scripts/jQuery.js"></script>
<style type="text/css">
.user {
cursor: pointer;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
//$(".user").live("click",function() {
$(".user").click(function() {
data = "id=Hello"
$.ajax({
type:"GET",
url:"demo.php",
data:data,
dataType:"html",
beforeSend:function(html){
},
success: function(callBack){
$(".cap").html(callBack);
console.log(callBack);
},
error: function(page_data){
},
});
});
});
</script>
</head>
<body>
<div id="container">
<div class="cap">Hello</div>
<span class="user">Add User</span>
</div>
</body>
</html>
This is the line returned from the other page that wont get loaded into the div.
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LcXq-oSAAAAAOgwSIew_xq6xkv1QUxrZbRcMz6n"></script>
The entire data returned
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LcXq-oSAAAAAOgwSIew_xq6xkv1QUxrZbRcMz6n"></script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=6LcXq-oSAAAAAOgwSIew_xq6xkv1QUxrZbRcMz6n" height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>
It is not possible to return "script" tags, even in a string format. This is because of the way javascript works. You could however let your demo.php script return json. You should look that up yourself, but I don't believe there is another way to do this easily.
(well, worse practice, you could just return a string and parse the values out of it but ...). Anyway, if you assume you return a json object in the following format, you could just copy paste the rest in your success function and alter the object name to 'callback' ...
var json = {
type: "text/javascript",
scriptSrc: "http://www.google.com/recaptcha/api/challenge?k=6LcXq-oSAAAAAOgwSIew_xq6xkv1QUxrZbRcMz6n",
id: "theFrame",
iframeSrc: 'http://www.google.com/recaptcha/api/noscript?k=6LcXq-oSAAAAAOgwSIew_xq6xkv1QUxrZbRcMz6n'
};
var script = document.createElement('script');
script.type = json.type;
script.src = json.scriptSrc;
$('<iframe>', {
src: json.iframeSrc,
id: json.id,
frameborder: 0,
height: 300,
width: 500
}).appendTo('.cap');
jsFiddle: http://jsfiddle.net/vT3Ug/1/
It actually is possible to render javascript tags. e.g. html5 boilertemplate uses it to see if it should load jQuery local or from CDN.
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>
As you can see if you inject script tags you need to escape the closing script tag <\/script>.
On the other hand, you can't do this
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=6LcXq-oSAAAAAOgwSIew_xq6xkv1QUxrZbRcMz6n" height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>
simply because you do not own the iframe source, your iframe won't get rendered.
But you could do something like this:
callBack = callBack.replace('</script','<\/script')
.replace('<noscript>','')
.replace('</noscript>','');
$('body').append(callback);

Calling a jQuery script on the parent page when clicking on a button in an iframe

I have a page that contains a dynamically loaded <iframe>. This iframe contains a <form>.
When the user submits the <form>, a new page is loaded within the <iframe> saying Thanks.
Instead of using a second page, i want the parent page to show the Thanks message.
So basically i am in my <iframe>, and i want to call a function on the parent frame.
How can i do that ?
From an iframe, it is possible to call a function declared on the parent window using window.top.functionName(). But for this to work, both pages should be hosted on the same domain. They are subject to Same Origin Policy. That means if you want to test this example, you should use a server (wamp, xampp, etc..).
page.html (updated)
<html>
<head>
<title>Page 1</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
window.foo = function(){
alert('page 1 is executing this code');
}
$(function(){
$('body').append('<iframe src="iframeContent.html"></iframe>');
});
</script>
</head>
<body></body>
</html>
iframeContent.html
<html>
<head>
<title>Page 2</title>
</head>
<body>
i am the iframe. This is my button.
<button onclick="javascript:window.top.foo()">click me</button>
</body>
</html>
I've done a little example for you.
Here is the main page (index.html) containing the iframe
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<input type="hidden" id="infoFormPosted" value=0 />
<iframe src="./iframe.html"></iframe>
<div id="thanks" style="display:none;">
Thanks!
</div>
</body>
</html>
And here is the iframe (iframe.html):
<html>
<head>
<title>myIframe</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#infoForm").submit(function() {
//change the value of the top document input hidden to 1
$("#infoFormPosted", top.document).val(1);
});
// if the input hidden is set to 1
if($("#infoFormPosted", top.document).val() == 1){
// show the thanks div on the top document (not in the iframe)
$("#thanks", top.document).show();
}
});
</script>
</head>
<body>
<div id="anydiv">
<form id="infoForm" action="#" method="post">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>
EDIT : An alternative is to create a function inside the main page and call it in the iframe using parent.nameOfTheFunction()
The main page (index.html)
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
showThanks = function(){
$("#thanks").show();
};
</script>
</head>
<body>
<input type="hidden" id="infoFormPosted" value=0 />
<iframe src="./iframe.html"></iframe>
<div id="thanks" style="display:none;">
Thanks!
</div>
</body>
</html>
The iframe (iframe.html):
<html>
<head>
<title>myIframe</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#infoForm").submit(function() {
//change the value of the top document input hidden to 1
$("#infoFormPosted", top.document).val(1);
});
// if the input hidden is set to 1
if($("#infoFormPosted", top.document).val() == 1){
// call showThanks function from the main frame
parent.showThanks();
}
});
</script>
</head>
<body>
<div id="anydiv">
<form id="infoForm" action="#" method="post">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>

Problem with the innerHTML the content of a DIV

For some reason, I can't change the contents of a DIV in either IE or Firefox. Any ideas where I might be going wrong?
JavaScript
function displayTable() {
document.getElementById('retailerDiv').innerHTML = '<a>HEY!</a>';
}
HTML
<input type="text" id="userQuery" onkeydown="displayTable()"/>
<br/>
<div id="retailerDiv">
Now
</div>
The code you posted works fine. Perhaps edit and post all of your source, or link to your page on the web. I don't have any problems with this working in Firefox 3.5 or IE8:
<HTML>
<HEAD>
<script type="text/javascript">
function displayTable()
{
document.getElementById('retailerDiv').innerHTML = '<a>HEY!</a>';
}
</script>
</HEAD>
<BODY>
<input type="text" id="userQuery" onkeydown="displayTable()"/>
<br/>
<div id="retailerDiv">
Now
</div>
</BODY>
</HTML>

Categories