Trouble calling Javascript function within markup - javascript

I'm having trouble getting my Javascript to run.
My javascript is located within the body tag of my master file (the layout page)
<script type="text/javascript">
var cssdropdown = {
startchrome2: function() {
document.getElementById("P3").innerHTML = "3 is complete";
}
}
</script>
I am attempting to call the startchrome2 function as follows:
<body onload="cssDropDown.startchrome2()">
The element I am attempting to access in my getElementById call is also located within the body:
<p id="P3">This is a paragraph.</p>
It is currently displaying "This is a paragraph". Why is my javascript failing to change it to "3 is complete"?
I've been fooling around with this for a while and it's quite frustrating. I'm sure it's a stupid error.
I am working in Visual Studio 2010 and the following are the declarations at the top of the page:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs"
Inherits="SiteMaster" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

Your casing is wrong, try:
<body onload="cssdropdown.startchrome2()">

Related

How to excecute a <p:remoteCommand> from javascript

I have a trouble when I try to excecute a remote command from a javascript code, always in the console I have the erro, ReferenceError: notificar is not defined and I don’t know why, this is my code, thanks
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Facelets
<p:growl id="notifyGrowl" widgetVar="notifyGrowl" life="3000" showDetail="true"/>
<h:form>
<p:remoteCommand name="notificar" actionListener="#{remoteCommandView.execute}" update="notifyGrowl" />
</h:form>
</h:body>
<script type="text/javascript">
if (window.WebSocket) {
var ws = new WebSocket("ws://localhost:8080/SEIPA3/push");
} else {
console.log(" Browser doesn't support it");
}
ws.onmessage = function (event) {
notificar();
};
</script>
Guys I solved this problem, what happened was that I ran my application and as I was in the browser, I went to my client directly from the url by typing the address where he is staying, doing an inspection of the source code of the page of the cliene I realize that I was still showing the code of primefaces as such, which is not right, should have been processed and displayed as pure html, so put a button from my index to send me to the client, so that now when doing the inspection showed me the html code already processed, and everything went well. thanks.

Datepicker isn't working inside search popup

I've been working with Struts2 and it's JQuery plugin for around a week and I'm a little bit lost.
Last thing I tried to do was to implement searches by date in a jqGrid I'm displaying on a page. For this, I followed this tutorial here.
The thing is it's not working because when I click on the searchfield which is supposed to pop out the datepicker, it won't pop out anything.
I've debugged the javascript code and found that when it tries to call the datepicker() function, an error comes up saying "Uncaught TypeError: Undefined is not a function" .
I'm not sure why this happens as I'm using Struts2-jquery-plugin 3.7.1. I'm posting my JSP code below (I've omitted all the grid rows that don't relate to the question):
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<%# taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<sj:head jqueryui="true" jquerytheme="south-street" locale="es" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
datePick = function(elem) {
$(elem).datepicker({
firstDay : 1
});
$('#ui-datepicker-div').css("z-index", 2000);
}
</script>
<title>Testing</title>
</head>
<body>
<s:url var="remoteurl" action="reservationList"/>
<div id="grid">
<sjg:grid
id="reservationsGrid"
caption="%{getText('reservationTable.title')}"
dataType="json"
href="%{remoteurl}"
pager="true"
gridModel="gridModel"
rowList="10,15,30"
rowNum="15"
navigator="true"
navigatorSearch="true"
autowidth="true"
navigatorSearchOptions="{multipleSearch:true, closeAfterSearch:true}">
...
<sjg:gridColumn name="date" index="date" title="Date" search="true" formatter="date" sortable="true" formatoptions="{newformat : 'd/m/Y H:i', srcformat : 'Y-m-d H:i'}" searchoptions="{sopt:['eq','lt','le','gt','ge'], dataInit:datePick}"/>
...
</sjg:grid>
</div>
</body>
</html>
Am I missing any import/reference or such a thing?
UPDATE
Recently I've found a hack, and it's telling me that the issue relates to the datepicker's import/reference:
All I did was adding a new tag inside my JSP:
<sj:datepicker style="display:none" disabled="true"></sj:datepicker>
By doing this, I guess I'm forcing the framework to automatically import and initialize a datepicker, and so it works, but it's not the solution I'm searching for.
So my question then is:
How can I import/reference and initialize the datepicker?
By default <sj:head> will NOT load all jQuery ui resources rather they are loaded on demand. When you've added a <sj:datepicker> tag it also loaded needed resources and your script was able to run.
In order to load all resources at once set loadAtOnce attribute of <sj:head> tag to true.
<sj:head jqueryui="true" loadAtOnce="true"
jquerytheme="south-street" locale="es" />

org.apache.jasper.JasperException: Unable to compile class for JSP:

I am trying to call jsp code in javascript.
The error is as follow
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 16 in the jsp file: /testng_index.jsp
String cannot be resolved
13: var mai=document.getElementById("j");
14: //mai.value = mai.value.toUpperCase();
15: var m=mai.value;
16: <%=String value=document.writeln(m)%>
17: var mo= <%= new PlaneBo().getOwnerId(value)%>;
18: // document.writeln(mo);
19: if(mo==0)
here is the code
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.ams.services.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" language="javascript">
function emailCheck()
{
var mai=document.getElementById("j");
//mai.value = mai.value.toUpperCase();
var m=mai.value;
<%=String value=document.writeln(m)%>
var mo= <%= new PlaneBo().getOwnerId(value)%>;
// document.writeln(mo);
if(mo==0)
{
// document.writeln(m);
var tab = document.getElementById("t");
var row = tab.insertRow(3);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var inpt= document.createElement("input");
inpt.setAttribute("name","jho");
inpt.setAttribute("type","text");
cell1.innerHTML="Name";
Please provide me suitable solution of this problem.
It looks to me as if you have some confusion about when and where Java and JavaScript code executes.
The Java code and JSP runs on the server when the browser requests the page. The server knows nothing about HTML and JavaScript. As far as the server is concerned, this is your JSP page:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.ams.services.*" %>
TEXT
<%=String value=document.writeln(m)%>
TEXT <%= new PlaneBo().getOwnerId(value)%>
TEXT
The server doesn't really care what's in the sections marked TEXT; it just sends them straight to the browser as they are. But it does care about what's in the <%# ... %> and <%= ... %> tags.
You get an error because the server doesn't understand document.writeln(m). It's not aware of any object named document, so it can't evaluate document.writeln(m). It happens that in JavaScript there is an object document and a function document.writeln, but that's irrelevant. The server doesn't know anything about JavaScript.
The JavaScript code doesn't execute until the page has finished being sent to the browser. You haven't shown when the function is called: it may be after an input field's value has been changed or a button has been clicked. The JavaScript runs in the browser, not on the server, so it can't directly call the Java code on the server.
If you really want to execute some Java code on the server during a call to your JavaScript function, you will need to use an AJAX call. See this question for more information on how to do this.

google chrome iframe body onLoad not working?

EDIT: It does work (sorry). Something in this script is causing it to stop in google chrome:
function checkLocation() {
var loc = top.location.href;
var suffix = "subpage.html";
if (loc.indexOf(suffix, loc.length - suffix.length) !== -1) {
top.location.href = "index.html";
}
}
Original post:
I have IE 9, FF 3.6.3, Chrome (18.0.1025.151) and Safari 5.1.5 all installed.
This works in all of the browsers except google chrome.
I have a HTML layout which contains a named iframe. The iframe src changes to display the different pages. On one of the pages I have a script which is loaded onLoad in the body tag. This script doesn't load when the page is loaded in the iframe in google chrome only - it works fine in other browsers. Also, if I load the page directly into google chrome (not via an iframe) it works just fine.
How do I fix this?
Here is an example code:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html> <head> <title> Example Page </title> </head>
<body> View subpage<BR/>
<iframe name="targetFrame"> </iframe>
</body>
</html>
subpage.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html> <head> <title> Subpage </title>
<script type="text/javascript" src="subpage.js"> </script>
</head>
<body onLoad="initialise()"> Hello </body>
</html>
subpage.js
function initialise() {
alert("Script loaded.");
}
Thanks for looking.
Turns out its just a security exception
Unsafe JavaScript attempt to access frame
with URL file:///C:/Users/.../website/index.html
from frame
with URL file:///C:/Users/.../website/subfolder/subpage.html.
Domains, protocols and ports must match.
Once its online it should be fine.

Google Docs Viewer gives javascript error inside ASP.Net page

I'm trying to embed google docs viewer in my page.
Here's the test.aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="Web_Application.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<iframe id="documentLoader" src="https://docs.google.com/viewer?embedded=true&url=http://view.samurajdata.se/license.pdf"
style="width:100%; height:800px;">
</iframe>
</body>
</html>
Here's my test.aspx.cs
namespace Web_Application
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
I get errors such as
'Line: 409
Error: Unable to get value of the property 'k': object is null or undefined'
or
'Line: 458
Error: Unable to get value of the property 'a': object is null or undefined'
which are from google doc viewer online javascript resource...
So I embedded the script in the header and it worked. But the problem is, if I do this in a user control or in a page inherited from a master page I get those errors again.
How can I solve this?
Thank you.
try this iframe script independent of any browser or version this will work ....
<iframe id="documentLoader" src="https://docs.google.com/viewer?url=http://view.samurajdata.se/license.pdf&chrome=true" style="width: 100%; height: 800px;">
</iframe>
mark yes if works !!! :)
<iframe id="documentLoader"
src="https://docs.google.com/viewer?url=http//view.samurajdata.se/license.pdf&embedded=true"
style="width:100%; height:800px;">

Categories

Resources