JS not calling function - constructor

Here is my calling html code
<!DOCTYPE html>
<html>
<head>
<title>Display Card Type</title>
</head>
<script src='Card.js'; ></script>
<body>
<script>
var card = new myCardType();
var value = new myCardType();
card.setCard('I am a club');
value.setFace('I am an Ace');
card.showSuit();
value.showFace();
</script>
</body>
</html>
And here is the separate function (saved in same folder).
function myCardType(){
this.suit = suit;
this.showSuit = function(){
alert(this.suit);
}
this.face = value;
this.showFace = function() {
alert(this.face);
}
this.setCard = function (newSuit) {
this.suit = newSuit;
}
this.setFace = function (newValue) {
this.face = newValue;
}
}
Spent ages reviewing this but just cannot see what is going on, grateful for any clues.

Related

How do I make a link open in a new tab with the url shown as "about:blank"

When I click the "click now" button nothing happens, I want it to open in a new tab. Code:
<button onclick="myFunction()">click to play</button>
<script>
var urlObj = new window.URL(window.location.href);
var url = "https://incognito42.herokuapp.com/src/gs/public/hexgl/";
if (url) {
var win;
document.querySelector('button').onclick = function()
I've tried rewriting it like this:
<html>
<body>
<button onclick="myFunction()">click to play</button>
<script>
function myFunction() {
var win = window.open(
"https://incognito42.herokuapp.com/src/gs/public/vex5/",
"DescriptiveWindowName",
"width=1920,height=1080,resizable,scrollbars=no,status=1"
);
myWindow.document.write('<html><head> <title>Sample</title><link rel="stylesheet" type="text/css" href="css/newsCSSWindow.css"></head><body>');
myWindow.document.write("Sample Window");
myWindow.document.write('</body></html>');
}
</script>
</body>
</html>
but then it doesn't open in a new tab with the URL shown as "about:blank", and just to be clear I want the URL bar to say "about:blank", but it displaying the link's contents shown on the page.
this page for reference
You can't.
While you could fix your trivial errors:
The URL you entered as the first argument to window.open isn't about:blank
You forgot the , after that first argument
You have var win and then myWindow.document.write
… the browser will change the displayed URL to reflect where the data from.
Browsers do not let webpages deceive users about the source of what they are looking at.
I suspect that you meant it if you do not write in the comment exactly what
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button onclick="myFunction()">click to play</button>
<script>
function myFunction(){
var urlObj = new window.URL(window.location.href);
var url = "https://incognito42.herokuapp.com/src/gs/public/hexgl/";
if (url) {
var win;
document.querySelector('button').onclick = window.open(url)}
}
</script>
</body>
</html>
Edited version
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button onclick="myFunction()">click to play</button>
<script>
var urlObj = new window.URL(window.location.href);
var url = "https://incognito42.herokuapp.com/src/gs/public/hexgl/";
if (url) {
var win;
document.querySelector('button').onclick = function() {
if (win) {
win.focus();
}
else {
win = window.open();
win.document.body.style.margin = '0'; //Set margin new blank window
win.document.body.style.height = '100vh'; //Set height new blank window
var iframe = win.document.createElement('iframe'); //Creates a frame for the game
iframe.style.border = 'none';
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.margin = '0';
iframe.src = url;
win.document.body.appendChild(iframe); //add a frame to the window
}
};
}
</script>
</body>
</html>

How can I use google.script.run properly?

I've been recently working on a project that sends values to a google spreadsheet and
the failure handler always gives me an alert, am I doing something wrong?
By the way, here's the code:
MainGsFile.gs:
var Server = SpreadsheetApp.openByUrl("Spreadsheet Url");
//I know that's not the Spreadsheet Url
var Pages = Server.getSheets();
function doGet()
{
return HtmlService.createHtmlOutputFromFile("MainHtmlFile");
}
function SetCellValue(PageNum, Row, Column, Value)
{
Page[PageNum].getRange(Row, Column).setValue(Value);
}
function GetCellValue(PageNum, Row, Column)
{
return Page[PageNum].getRange(Row, Column).getValue();
}
MainHtmlFile.html:
<!DOCTYPE html>
<!--HTML Code-->
<html>
<body>
<button onclick="Test()">It's going to work</button>
</body>
</html>
<!--CSS Code-->
<style>
//Styles go here
</style>
<!--JavaScript Code-->
<script>
function Test()
{
google.script.run.withSuccessHandler(OnSuccess).withFailureHandler(OnFailure).SetCellValue(1, 1, 1, "true");
}
function OnSuccess()
{
alert("Yes");
}
function OnFailure()
{
alert("No");
}
</script>
Yes,
You can't pass mutiples arguments. Trick is to use object :
var obj = {};
obj.value = 1;
obj.value2 = 2;
obj.value3 = 3;
obj.bool = true;
google.script.run.withSuccessHandler(OnSuccess).withFailureHandler(OnFailure).SetCellValue(obj);

Contents of a spreadsheet are not displayed in the deployed Apps Script webapp

I've tried to do an example of the 3rd tutorial on this ctrlq page, in which we display the content of a spreadsheet on a web page. Here are my files which I simply entered - I didn't activate anything, I just did Publish -> Deploy as web app
I expect to see all of the spreadsheet's values, but instead nothing is shown:
Code.gs
function doGet() {
var html = HtmlService.createTemplateFromFile("index").evaluate();
html.setTitle("Dynamic Webpage");
return html;
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.getContent();
}
function getData(){
var sheet = SpreadsheetApp.openById("1k_kj98U__0Bk44gh0qRFpaVx0ru3sN1pSPGiMQwimxo").getSheets()[0];
return sheet.getDataRange().getValues();
}
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<?!= include('script_js'); ?>
<body>
<div id="data"></div>
</body>
</html>
script_js.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>
window.onload = function(){
google.script.run.withSuccessHandler(showData).getData();
}
function showData(data){
var html = "";
for (var i=0; i<data.length; i++) {
html += "<br>" + data[i].join(":");
}
document.getElementById("data").innerHTML = html;
}
</script>
</body>
</html>
Link to the script project
EDIT as #tehhowch pointed out his answer is the correct solution here.
For your case you should make these changes, getData():
function getData(){
var sheet = SpreadsheetApp.openById("1k_kj98U__0Bk44gh0qRFpaVx0ru3sN1pSPGiMQwimxo").getSheets()[0];
return JSON.stringify(sheet.getDataRange().getValues()); // return value as Json
}
And showData(data):
function showData(data) {
var arr = JSON.parse(data);
var html = "";
for (var i=0; i < arr.length; i++) {
html += "<br>" + arr[i].join(":");
}
document.getElementById("data").innerHTML = html;
}

Passing message from one window to another window

I'm trying to pass the message from comn.html window to comm.html window, but it doesn't pass
please tell me whether the code is correct or not. If not, kindly provide the exact code for that.
I want the code for passing message between windows not between iframes.
Code of comn.html:
<html>
<head>
<script>
window.onload = function()
{
var btn = document.getElementById('send');
btn.addEventListener('click', function sendMessage(e)
{
var string="Hi Adaptavant";
var new_win=window.open('comm.html');
new_win.postMessage(string,"*");
});
}
</script>
</head>
<body>
<button id="send" >Send Message</button>
</body>
</html>
Code of comm.html:
<html>
<head>
<title>postMessage</title>
<script type=text/javascript>
window.onload = function()
{
var msg = document.getElementById('message');
window.addEventListener("message",function receiveMessage(e) {
msg.innerHTML="msg received= " +e.data;
document.write("the message is " +msg);
}
</script>
</head>
<body>
<div id="message"></div>
</body>
</html>
If I run this program, my message isn't posting in another window.
What's the problem?
replace your script with this:
window.onload = function()
{
var msg = document.getElementById('message');
window.addEventListener('message', function(e) {
var message = e.data;
msg.innerHTML="msg received= " +e.data;
});
}
Let me know if it worked!

I can't acquire JSON data AngularJS

I want to get THE MOVIE REVIEWS API by The New York Times. I tried it using AngularJS but I was not provided.Can someone help me?
var app = angular.module('movieApp', []);
app.controller('movieController',
function movieController($scope, $http) {
$scope.fetchReviews = function() {
var api = 'http://api.nytimes.com/svc/movies/v2/reviews/all.jsonp?&offset=20&order=by-opening-date&api-key=XXX MY KEY XXX&responce-format=.jsonp&callback=JSON_CALLBACK';
$http.jsonp(api).success(function(data){
$scope.results = data.results;
});
}
});
HTML
<!DOCTYPE html>
<html lang="en" ng-app="movieApp">
<head>
<meta charset="UTF-8">
<title>Movie Review</title>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</head>
<body>
<div ng-controller="movieController">
<ul class="review-cards">
<li ng-repeat="item in results">
<h2>{{item.display_title}}</h2>
<p>{{item.summary_short}}</p>
</li>
</ul>
</div>
</body>
</html>
You have to handle that event to some element or just call it in your controller.
var app = angular.module('movieApp', []);
app.controller('movieController',
function movieController($scope, $http) {
// define default value
$scope.results = [];
$scope.fetchReviews = function() {
var api = 'http://api.nytimes.com/svc/movies/v2/reviews/all.jsonp?&offset=20&order=by-opening-date&api-key=XXX MY KEY XXX&responce-format=.jsonp&callback=JSON_CALLBACK';
$http.jsonp(api).success(function(data){
$scope.results = data.results;
});
}
// call that event
$scope.fetchReviews();
});
Try it.
app.controller('movieController', function ($scope, $http) {
$scope.fetchReviews = function() {
var api = 'http://api.nytimes.com/svc/movies/v2/reviews/all.jsonp?&offset=20&order=by-opening-date&api-key=XXX MY KEY XXX&responce-format=.jsonp&callback=JSON_CALLBACK';
$http.jsonp(api).success(function(data){
$scope.results = data.results;
});
}
$scope.fetchReviews();
});
check like this