Is this the right code for a pop up window? - debugging

I wanted this code to pop up a window when the password is correct and when the password isn't correct I wanted to make it go to google.com.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script language="Javascript">
<!--hide
var password;
var pass1="12345";
<a href="https://www.google.com/?safe=active&ssui=on"
target="_blank" onclick="preventProp(event)">Google</a>
function preventProp(e){
if(confirm("Are you sure you want to open this link ?")){
return true;
}
password=prompt("Enter Password To View Zapazzi","");
if(password!==pass1)
window.confirm("Incorrect Password, click ok to go to Google");
else
{
alert("Correct Password, click ok to go to Zapazzi");
window.location="https://sites.google.com/cusdk8.org/zapazzi/home";
}
//-->
</script>
</body>
</html>

here is what you have to do
remove the <a href="https://www.google.com/?safe=active&ssui=on"
target="_blank" onclick="preventProp(event)">Google</a> from <script> </script>
change script type to <script type="text/javascript">
change the <a href="...> to button because when it is a link tag it firstly does its job of redirecting before listening to the event so I used button
here is my clean code that is working
<!DOCTYPE HTML>
<HTML>
<head>
</head>
<body>
<button onclick="preventProp(event)">Google</button>
<script type="text/javascript">
var password;
var pass1="12345";
function preventProp(e){
if(confirm("Are you sure you want to open this link ?")){
return true;
}
password=prompt("Enter Password To View Zapazzi","");
if(password !== pass1)
window.confirm("Incorrect Password, click ok to go to Google");
else{
alert("Correct Password, click ok to go to Zapazzi");
window.location="https://sites.google.com/cusdk8.org/zapazzi/home";
}
}
</script>
</body>
</html>

Related

Jasmine: How to test a user input?

I wrote the following html file:
<!DOCTYPE html> <html lang="en"> <head>
<meta charset="UTF-8">
<title></title> </head> <body>
<h1> HELLO WORLD </h1>
<button type="button"> CLICK HERE</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$('button').click(function() {
$('h1').text("Thank you");
var ans = prompt("Please type your age");
if (ans > 80) {
$('h1').text("You're old");
}
})
</script> </body> </html>
I want to test that once the user clicks on the button and gives an age older than 80, the page behaves as expected and the h1-tag text changes to "You're old".
My question is how to write a Jasmine spec to test this feature?
You need to use "Spy" object to intercept "prompt" function and return your own value. After check the caption text. The code may look like ...
deascribe("test inline function:", function() {
it("caption should be 'too old' for age of more than 80", function () {
spyOn(window, 'prompt').and.returnValue(81);
$('button').click();
expect($('h1').text()).toBe("You're old");
});
});

d3.xhr example or AJAX d3's way

I've been looking for an example of updating data with d3.xhr but have not seen anything obvious, or at least of my level of understanding. The following 2 links are close but no cigar:
from stackoverflow
from mbostock’s block
I look around more and found this example in jquery and php. I tried it and understand the code. I would appreciate if you give me an equivalent code in d3.xhr or d3.json. BTW, what is different between d3.xhr and d3.json, and when to use what? Thanks in advance.
<?php
// AJAX & PHP example
// http://iviewsource.com/codingtutorials/learning-how-to-use-jquery-ajax-with-php-video-tutorial/
if ($_GET['ip']) {
$ip = gethostbyname($_GET['ip']);
echo($ip);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Get Reverse IP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
</head>
<body>
Please enter a domain name
<input type="text" id="searchip">
<div id="resultip"></div>
<script>
$(document).ready(function() {
$('#searchip').change(function(){
$.ajax({
type: "GET",
url: "ajax.php",
data: 'ip=' + $('#searchip').val(),
success: function(msg){
$('#resultip').html(msg);
}
}); // Ajax Call
}); //event handler
}); //document.ready
</script>
</body>
</html>
I found the solution. In fact it has less coding, :) I sincerely hope that the answer would be useful to someone.
<?php
// AJAX & PHP example
if ($_GET['ip']) {
$ip = gethostbyname($_GET['ip']);
echo($ip);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Get Reverse IP</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
Please enter a domain name
<input type="text" id="searchip">
<div id="resultip"></div>
<script>
d3.select('#searchip').on("change", function() {
d3.xhr('xhr.php?ip='+this.value, function(data) {
d3.select('#resultip').html(data.response);
})
});
</script>
</body>
</html>

How To display text when we click button

<html>
<head>
<script>
function loadXMLDoc()
{
Welcome To Ajax
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
How to display text when we click button. How to display text when we click button?
function loadXMLDoc()
{
document.getElementById("myDiv").innerHTML = "<h2>Welcome To Ajax</h2>"
}

AJax will not display a text file onto the screen?

I am new to AJAX and am experimenting with a simple page that should show a txt file into a specific location. At this point it does not show the vlist.txt file.
Question: Why does this code not display the file (vlist.txt)?
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("C:\Users\itpr13266\Desktop\vlist.txt");
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2>This is where your text will go</div>
<button>Get External Content</button>
</body>
</html>
New Code that was Tried.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load(function() {alert( "Load was performed." ););
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2>This is where your text will go</div>
<button>Get External Content</button>
</body>
</html>
No alert box was displayed so I am not sure that it is even getting to that point.
$().load() can't be used with a local file path like C:\Users\itpr13266\Desktop\vlist.txt.
Put your file on a web server and your code will most likely work.

new Ajax.Request to redirect to new jsp fails.

I have 2 login pages, login.jsp and loginMobile.jsp. Login.jsp is default login page and configured in web.xml.
On entring login.jsp a javascript method isMobile is called.
WHITHOUT CHANGING the URL, need to forward the request to loginMobile.jsp.
FRAMEWORK : struts1 , AJAX + prototype + GLASSFISH 3.1.2
2 login pages :
login.jsp
<script src="scripts/prototype.js" type="text/javascript"></script>
<script src="scripts/login.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
isMobile();
</script>
<html>
<body>
<p> AM IN LOGIN PAGE </p>
</body>
</html>
loginMobile.jsp
<script src="scripts/prototype.js" type="text/javascript"></script>
<script src="scripts/login.js" type="text/javascript"></script>
<html>
<body>
<p> AM IN MOBILE PAGE </p>
</body>
</html>
Inside login.js
function isMobile()
{
//check some conditions then call below AJAX
new Ajax.Request('/loginMobile.jsp', { method:'post' });
}
//Complain's FILE NOT FOUND !!
"C:\Users\dummy\AppData\Roaming\NetBeans\7.2.1\config\GF3\domain1\docroot\loginMobile.jsp" not found
ALSO TRIED
function isMobile()
{
//check some conditions then call below AJAX
var athena_login_url = window.location.href+'/loginMobile.jsp';
new Ajax.Request(athena_login_url, { method:'post' });
}
// Just goes to default login.jsp
Any suggestion is appreciated. Thank you in advance.

Resources