Invoke javascript function in parent page from child page - javascript

I need to call a function in the main page from the child page. Below i have 2 pages. 1. main.aspx and child page is 2.active.aspx. In active.aspx i have a button called "exit' . when i click on the exit button , i need to close the modal popup and then i need to call function "main" which is a javascript function in the main page. Problem is i am not able to invoke the function from the child page.
Main.aspx
<html>
<head>
<script type="text/javascript">
function main() {
//do main work
}
</script>
</head>
<body>
<div>
<input type="button" class="ic-grid-encode center-btn" title="Interactive"
data-toggle="modal" data-target="#divInterActiveContent" />
</div>
<div class="modal fade" id="divInterActiveContent" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document" align-self="" center;"="">
<iframe id="interId" src="active.aspx"></iframe>
</div>
</div>
</body>
</html>
Active.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<link href="../Content/bootstrap.css" rel="stylesheet">
<script src="../Scripts/jquery-3.1.1.js"></script>
<script src="../Scripts/bootstrap.js"></script>
<script src="../Scripts/knockout-3.4.2.js"></script>
<script src="../Scripts/jquery-ui-1.12.1.min.js"></script>
<script type="text/javascript">
self.btnExit = function () {
parent.$('#divInterActiveContent').modal('hide');
//after closing the popup i need to call the javascript function defined in the main page
//i.e i need call function main which is there in the main window.
}
</script>
</head>
<body>
<form id="frmInterActiveEncode">
<div class="container">
<div class="row" style="margin-top: 10px;">
</div>
<hr>
<br>
<hr>
<div class="row">
<input type="button" value="Submit" class="btn btn-primary" data-bind="event: { click: btnSubmit }">
<input type="button" value="Exit" class="btn btn-primary" data-bind="event: { click: btnExit }">
</div>
</div>
</form>
</body>
</html>

In your scenario, you are actually closing the modal first i.e you are closing the iframe also so you are not able to get parent object in child page after closing the modal.
You should do like
active.aspx
self.btnExit = function () {
parent.main();
}
main.aspx
function main(){
$('#divInterActiveContent').modal('hide');
// Do your stuff
}
This way you can call the parent function and instead of hiding the modal in child hide it in parent function and your next stuff you want to do.

Related

ReferenceError: button is not defined

I have a simple bootstrap page. It is the beginning of a bigger projects for kids where I will be asking them to click on a button to start a quiz. I am not able to display the quiz using the Javascript: document.getElementById("message").innerHTML = "Quiz String";. I am trying to do that using addEventListener. I am not sure what went wrong. Here is my entire code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quiz Page.</title>
<!-- Bootstrap -->
<link href="css/bootstrap-4.2.1.css" rel="stylesheet">
<style>
.TopDivMarg {
margin-bottom: 50px;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-12 TopDivMarg"></div>
</div>
<!-- Quiz Group -->
<div class="row">
<div class="col-xl-4"></div>
<div class="col-xl-4">
<p id="message" class="text-center">Click Button to Start Quiz.</p>
</div>
<div class="col-xl-4"></div>
</div>
<!-- Button Group -->
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<button type="button" class="btn btn-info">Start Quiz</button>
</div>
<div class="col-md-4"></div>
</div>
<script>
button.addEventListener("click",
function (changeText){
document.getElementById("message").innerHTML = "What are the colors of the rainbow?";
});
</script>
There is no button assigned an id of 'message'
Start Quiz
Try this in the JS.
var submitButton = document.querySelector('.btn-info');
submitButton.addEventListener("click", function (changeText) {
document.getElementById("message").innerHTML = "What are the colors of the rainbow?";
});
We are search for a button with the class 'button-info' and adding the event listener to it.
As you said It is a big project. You should never add event listener to the bootstrap classes which won't be unique in your code going forward.
Use Id instead.
<button type="button" class="btn btn-info" id="btn-quiz-start">Start Quiz</button>
var submitButton = document.querySelector('#btn-quiz-start');
first need to get the reference of the button before add the event listener:
<!-- Button Group -->
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<button id="button" type="button" class="btn btn-info">Start Quiz</button>
</div>
<div class="col-md-4"></div>
</div>
<script>
const button = document.getElementById("button");
button.addEventListener("click", function(changeText){
document.getElementById("message").innerHTML =
"What are the colors of the rainbow?";
});
</script>

jquery .load() external content failed in modal

I try to load an external block of html through .load() jquery function, but it fails to render properly in a modal popup (note: same issue with .get() )
code is :
$('#butt2').on('click', function() {
$('#mbodyko').load('nav.html', function() {
$('#myModalnok').modal({show: true });
});
});
Render is bad, here is the issue:
http://plnkr.co/edit/z9oQPVG2F9oYaJCzovN5?p=preview
You can notice that the tabs inside the second modal are not clickable, whereas the same html code renders well in the first modal.
Do you know why ?
Thanks for your hints!
All I did to make it work is removing out the first modal html, like this:
<html dir="ltr" lang="en-US">
<head>
<link rel="stylesheet"
href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
</head>
<body>
<hr>
<button class="btn btn-primary btn-lg" id="butt1">modal OK</button>
<br />
<br />
<button class="btn btn-primary btn-lg" id="butt2">modal NOK - use jquery .load</button>
<div class="modal fade" id="myModalnok" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-body">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Problem: click does not change the tabs!</h4>
</div>
<div class="modal-body" id="mbodyko">
</div>
</div>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"> </script>
<script>
$('#butt1').on('click', function() {
$('#myModalok').modal({
show: true
});
});
$('#butt2').on('click', function() {
$('#mbodyko').load('nav.html', function() {
$('#myModalnok').modal({
show: true
});
});
});
</script>
</body>
</html>
I think the problem is that the loaded data from nav.html have IDs that are in the original html DOM, or index.html and you can't have a page with two elements that have the same ID, or it will not work properly.

How to show alert box after the link is click [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
Hello this is what i want to do. I want to show the alert box after the user click the link. My link is directed to another php page but it will come back again to the page where the link is found. How can i achieve this? Can someone give me ideas on how to do it?
this is what i tried but not working.
<?php
function fsa() {
echo '<div class="alert alert-success alert-dismissable" id="success-alert">';
echo '<strong>Successfully posted!</strong>';
echo '</div>';
}
<a class='btn btn-info left-margin' href="dbf-import.php" onclick="fsa()">Import Database</a>
This is an example to show the dissmissable alert box when clicking on a button.
$(document).ready(function() {
$('.activater').click(function() {
$('.alert').show()
})
});
.alert {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="container">
<h2>Dismissal Alert Messages</h2>
<button class="activater">Send Message</button>
<a class="activater">SHOW MESSAGE</a>
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Success! message sent successfully.
</div>
</div>
</body>
You can achieve it through Bootstrap modal instead of alert box. You can design it as according to your need.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
click on this link :
<!-- Trigger the modal with a button -->
<span data-toggle="modal" data-target="#myModal">www.google.com</span>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Alert Box. Click 'OK' to go to www.google.com</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" onClick="document.location.href='www.google.com'">Go</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
PHP is used to generate the HTML and JS. You can have both in a PHP file but you need to see the difference between PHP and JS. The function you've called fsa() is a JS function because it will run client side (on the client machine and after page has been loaded). The onclick param in the HTML tag needs to have a JS function hooked. This way when you click on your <a> tag, fsa() will be fired and the box will show up on the page.
// this is JS, you need <script> tags !!!
<script>
function fsa() {
// define a new variable
var box = '';
// add the HTML inside the JS variable
box += '<div class="alert alert-success alert-dismissable" id="success-alert">';
box += '<strong>Successfully posted!</strong>';
box += '</div>';
// append the HTML contained inside box to the body
document.querySelector('body').innerHTML += box;
}
</script>
<a class='btn btn-info left-margin' onclick="fsa()">Import Database</a>
If you wish to save the data without reloading the client's page, you may want to use AJAX
First of all, like #Iceman said: You are trying to run a javascript function in php, so start by deleting all that. If you just want the user to get an alertbox when clicking on a link, you can simply add
<script> function fsa(){
alert("Your alert message");
}
</script>
Also, if you don't want the person to leave the side, just put href='#', that should take care of it.
Try this
<script type="text/javascript">
function Alert() {
var answer = confirm ("Successfully posted!Please click on OK to continue.")
if (answer)
window.location="http://www.yoursite.com";
}
</script>
click me

Unable to Capture Bootstrap Model using Javascript

below is my HTML model,
<
And below is my JavaScript.
$(document).ready(function() {
alert("Script is on");
$("#portfolioModal1").on('show.bs.modal', function () {
alert('The modal is about to be shown.');
});
$( "#portfolioModal1" ).on('shown', function(){
alert("Shown!");
});
});
$("button").click(function find_route_time()
{
// This function is tested and is working fine.
});
I tried solution from many tutorials, but this is not working. I am unable to understand why? My javascript should generate alert when the Model Opens up but nothing happens.
And below is the link,
<a href="#portfolioModal1" class="portfolio-link" data-toggle="modal" data-target="#portfolioModal1">
<div class="portfolio-hover">
<div class="portfolio-hover-content">
<i class="fa fa-plus fa-3x"></i>
</div>
</div>
<img src="img/portfolio/EAST-CLICK.jpg" class="img-responsive" alt="">
</a>
Here is the fix for your code:
Add Button to your html code as showing below:
<button type="button" class="btn btn-info btn-lg" id="myBtn">Open Modal</button>
Replace your JS code with below code:
$(document).ready(function(){
$("#myBtn").click(function(){
$("#portfolioModal4").modal("show");
});
$("#portfolioModal4").on('show.bs.modal', function () {
alert('The modal is about to be shown.');
});
$("#portfolioModal4").on('shown.bs.modal', function () {
alert('The modal is fully shown.');
});
});
Note - if you want to open popup on page load event then just remove the click event from the script but the issue will be it just going to capture shown event.
let me know if you find this useful.
Here is completed Tested file.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
Open Popup
<!-- Modal -->
<div class="portfolio-modal modal fade" id="portfolioModal1" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-content">
<div class="close-modal" data-dismiss="modal">
<div class="lr">
<div class="rl">
</div>
</div>
.....................SOME CODE.....................
</div>
</div>
</div>
<script>
$(document).ready(function () {
alert("Script is on");
$("#portfolioModal1").on('show.bs.modal', function () {
alert('The modal is about to be shown.');
});
$("#portfolioModal1").on('shown.bs.modal', function () {
alert("Shown!");
});
});
$("button").click(function find_route_time() {
// This function is tested and is working fine.
});
</script>
</body>
</html>

AngularJS textbox return undefined value

When I press my button check in bootstrap modal, I want to print the value of my textbox in my console. But my textbox returns a undefined. It seems that all of the code are working perfect.
this is link Plunker
<!doctype html>
<html ng-app="app" ng-controller="ModalDemoCtrl">
<head>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
<script src="example1.js"></script>
</head>
<body>
<div ><button class="btn btn-default" ng-click="open('lg')" >Large modal</button>
<script type="text/ng-template" id="myModalContent">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<input type="text" ng-model="new" /><button ng-click="check()" >check</button>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
</div>
</body>
</html>
Since your modal is set to have its own controller, and therefore its own scope,
when you execute the function 'check' it tries to alert $scope.new,
$scope.new isn't set on the modal scope.
one way to overcome this is to pass the new variable into the function, and let the function alert the passed value.
here's a fixed plunkr
when calling check(), pass in the new variable:
<button ng-click="check(new)">check</button>
and in your controller change check function:
$scope.check = function(text) {
alert(text);
};

Categories

Resources