I don't know why I am getting this problem. I have used prototype before in javascript and it works fine but for some reason it is not working here:
test.html:
<script type="text/javascript">
$(document).ready(function(){
UserOptions("test");
});
</script>
UserOptions.js:
function UserOptions(username){
...
var userOptions = document.createElement("div");
userOptions.className = "userOptions";
**this.createBtns(userOptions);**
userContainer.appendChild(userOptions);
contentCenter.appendChild(userContainer);
contentCenter.appendChild(br);
BuddyList();
}
UserOptions.prototype = {
createBtns:function(parent){
var self = this;
/* Add Buddy Button */
var addBtnContainer = document.createElement("div");
addBtnContinaer.className = "addBtnContainer";
...}
I keep getting the error Object has no method 'createBtns'
You forgot the new keyword. Now it's trying to call your constructor as a regular function, and this will point to window or whatever.
new UserOptions("Fred");
Related
I am launching an html page when users click a button. I need to be able to pass in an array of addresses to the new window so that I can load them into a table, however I have been unable to find any way to pass an array over to the new window.
My most recent attempt looks like the following:
<button onclick="openWindow(['Joe Smith\n1 Address\nCity 12345',
'Joe Smith\n2 Address\nCity 12345'])">Button</button>
function openWindow(addresses){
var myWindow = window.open("NewPage.html");
myWindow.addresses = addresses;
}
And then in NewPage.html I've got:
<script type="text/javascript">
function bodyLoaded() { //gets called when the body loads
var addresses;
alert(addresses);
}
</script>
I always get undefined in the alert on the new window. I did confirm that I am getting the addresses if I set up an alert in the openWindow() function.
I've tried several other things as well, including using localStorage (How to pass an array to a new page using javascript?) altho I don't know if I did it correctly. I also tried executing the function to load the table on the new window from the openWindow function (passing in the addresses variable) but I keep getting errors saying "Error: Object doesn't support property or method". For example:
function openWindow(addresses){
var myWindow = window.open("NewPage.html");
myWindow.loadTable(addresses); //the loadTable function exists in a .js file
}
Ideally I just want to pass a single array to the new window but I've been stuck on this for a while now. Any assistance or suggestions would be greatly appreciated.
One possibility is to pass the array of params as a query in the url. So something like this:
var myWindow = window.open("NewPage.html?addresses[0]=Joe Smith\n1 Address\nCity 12345&addresses[1]=Joe Smith\n2 Address\nCity 12345");
Then in javascript using this function
<script type="text/javascript">
function bodyLoaded() { //gets called when the body loads
var urlParams = new URLSearchParams(window.location.search);
var addresses = urlParams.get('addresses'));
alert(addresses);
}
</script>
Edit:
Also localstorage works according to this answer: (How to pass an array to a new page using javascript?)
window.localStorage.setItem("cart", JSON.stringify(cart)); // Saving
var cart = JSON.parse(window.localStorage.getItem("cart")); // Retrieving
The new window is open by code so the code has some control over that window. Try something like this.
function openWindow(addresses) {
var myWindow = window.open("newpage.html");
myWindow.onload = function() {
var myDiv = this.document.createElement('div');
this.document.body.appendChild(myDiv);
for (var i = 0, a; a = addresses[i]; ++i) {
myDiv.innerHTML += a;
}
}
}
var com = {};
var com.Project = function(){
this.display = function(){
alert("hai.....");
}
}
var project_obj = new com.Project();
while creating the project_obj i got an error in IE9 like "Object doesn't support this action"
this code working well in firefox and chrome.
i have given a sample code.
i'm trying to use Classes and package concept in javastript.
i don't know why this error came in IE.
This is illegal in all browsers and raises a syntax error :
var com.Project = function(){
You may do this :
var com = {}; // whatever
com.Project = function(){
The problem is the 1st line, as variable names cannot include ..
If you're trying to namespace, you need to first define com as an Object with Project as one of its properties:
var com = {
Project: function () {
// etc.
}
};
Based on the working link to your javascript code I think you should change this
$.extend(true, window, container[0]);
to
$.extend(true, window, d);
OK so I fixed my last error with the DIV and all..
OK so at the very top of my javascript file... I have
$(function() {
var games = new Array(); // games array
});
and then in a new function I have: var gamesLgth = games.length;
but when I run it, I get this: Uncaught ReferenceError: games is not defined
When I weird because I initalized it at the very beginning...
games is out of scope. You need to store it somewhere such that your other function can access it.
For example, this will make your variable global.
var games;
$(function() {
games = new Array(); // games array
});
$(function() {
var gamesLgth = games.length;
console.log(gamesLgth);
});
By declaring that variable within a function you have scoped the variable to that function, which means that that variable games is only available within that function.
$(function() {
var games = new Array(); // games array
...
var gamesLength = games.length; // works fine
});
But this following example will not:
$(function() {
var games = new Array(); // games array
});
$(function() {
var gamesLength = games.length; // won't work - im in a different scope
});
You initialized it as a local variable of a separate function. You need to make it global, or to pass it from function to function as argument to be able to access it.
try this;
var games = [];
instead of
var games = new Array();
and also be sure about the games scope.
I'll go ahead and write the code out for you:
var games;
$(function(){
games = new Array();
...
});
$(function(){
games.length; // Which would be zero
});
You initialized "games" in a function scope, it's not available outside the borders of $(function() { ... });
I have an object which creates a slideshow.
I want to have several slideshows on a page
I have an event handler for slideshow element inside it
I want the event handler to know which slideshow object has created an item clicked
-
slideshow=function(){
var g = document.createElement(...);
...
g.onclick = f1;
f1 = function(slideshow_instance, event) {
//EXPLAIN ME HOW TO GET THE slideshow_instance
}
}
var sl1 = new slideshow();
var sl2 = new slideshow();
Clicking on an element slideshow has created should return either
sl1
or
sl2
I explain well?
Short answer, use: this.
Longer answer, what you want is:
slideshow=function(){
/* ... */
var self = this;
f1 = function(event) {
// do stuff with self.
}
}
The reason you need to point to this using self is that event handlers will change the meaning of this when they are called. But at the time the object is created this properly refer to the correct object (an instance of slideshow). The reason we can access the self variable during event callback is because it has been captured by a closure.
Feel free to google or search on stackoverflow any word/terminology from the above description if you need further explanation.
slideshow=function(){
var g = document.createElement(...);
g._objRef = this;
...
g.onclick = f1;
f1 = function(event) {
alert(this._objRef);
}
}
var sl1 = new slideshow();
var sl2 = new slideshow();
A bit of a hack imo, but it should do the trick.
Hi currently I'm trying to get following snippet of code to work:
function Entry() {
var pauses = new Array();
}
Entry.prototype = {
AddElement: function(aParameter) {
this.pauses.push(aParameter);
}
}
Unfortunately this code fails with following error in Safari if I try to call AddElement("Test");
TypeError: Result of expression 'this.pauses' [undefined] is not an object. Does anybody know why?
In your code, pauses is a local variable within the Entry() function, not a member on the object constructed by it.
You want to replace var pauses = ... with this.pauses = ....
change
var pauses = new Array();
to
this.pauses = new Array();
or, better
this.pauses = [];