Javascript Changing Class name error? - javascript

here is my code
var url = window.location.href;
if (url.substr(7, 21) === "www.codtelevision.com")
{
//Valid domain
if (url.substr(29) === "")
{
document.getElementById('homeLink').className = "active";
}
else
{
document.getElementById(url.substr(29) + "Link").className = "active";
}
}
When i look in my chrome developer tools, i see this error.
Uncaught TypeError: Cannot set property 'className' of null
I have tried so many times to fix it but can not. How do i get this to work?
Also, when i just put this in the console, it works:
document.getElementById('homeLink').className = "active";
My website is www.codtelevision.com

maybe your document is not loaded. put your code inside window.onload .
window.onload = function(){
//code
}
or if you are using jquery:
$(document).ready(function(){
//code
});

Related

Hiding HTML Element With Specific Class Unless URL Parameter is Present Using Javascript

Trying to hide an element (class of "input-container") with a specific class unless a specific URL parameter is present. Have worked out the following code, but can't seem to get it to work. The if/else statement has been tested using alerts, so I know that portion is working. It's where I'm trying to assign the display variable where the code seems to be breaking. Unfortunately, I have to do this using the class name rather than an ID. Thoughts?
<script>
var isadmin = document.getElementsByClassName("input-container");
if (window.location.search.indexOf('admin=yes') > -1) {
isadmin[0].style.display = "block";
} else {
isadmin[0].style.display = "none";
}
</script>
Your code seems alright. I wonder why is not working. Anyway I would try this
<script>
var isadmin = document.getElementsByClassName("input-container");
var url = new URL(window.location);
if (url.searchParams('test') && url.searchParams('test') == 'yes') {
isadmin[0].style.display = "block";
} else {
isadmin[0].style.display = "none";
}
</script>
As it turns out, it was an issue between the seat and the keyboard. I'm an idiot and didn't realize that the Javascript was executing prior to the elements loading. Fixed the issue with the following code...
<script>
window.onload = hidePass;
function hidePass() {
var isadmin = document.getElementsByClassName('input-container')[0];
if (window.location.search.indexOf('admin=yes') > -1) {
isadmin.style.display = "block";
} else {
isadmin.style.display = "none";
}
}
</script>

Is there any callback function after 'Friends List' loaded in cometchat?

I have integrated 'Embed Layout' comet chat on my site. Now I want to open particular friend chat on page load.
In the documentation, I've found below code to do the same. REF : Documentation Link
jqcc.cometchat.chatWith(user_id)
I have included in custom js from admin panel. However, it is showing below error in console
jqcc.cometchat.chatWith is not a function
But If I use same after friends list loaded from the console it is working fine.
How can I fix this issue?
Currently for time being I have fixed this issue by adding below code in custom js
var first_chat_loaded = false;
var first_chat = setInterval(function () {
try {
if (first_chat_loaded === false) {
// Function to get other user id defined in parent html page
var other_userid = parent.get_other_user_id();
jqcc.cometchat.chatWith(other_userid);
first_chat_loaded = true;
clear_first_load();
}
} catch (e) {
}
}, 1000);
function clear_first_load() {
clearInterval(first_chat);
}
Please let me know, If there is any proper way to do the same.
Kindly make use of this code snippet for the above mentioned issue
var checkfn = setInterval(
function(){
if(typeof jqcc.cometchat.chatWith == 'function'){
jqcc.cometchat.chatWith(user_id);
clearInterval(checkfn);
}
},
500);

unable to trigger window.open on js code

Im having issues with .js codes. the window.open seems to be not working. i have tried using window.location and not working also. Can anyone suggest what could be the fix for this?
Here's the code that im currently using
if ("web page" == $scope.model.ComponentModel.Screen.trnscntype) {
var staffId = ichrisCache.get("loginStaffNumber");
if ($scope.model.ComponentModel.FormCode == 'SEL') {
var menulinkParameters = { staffId: staffId, formCode: $scope.model.ComponentModel.FormCode };
$scope.retLink = "";
var menuLinks = SelfServiceLinkURL.query(menulinkParameters,
function (output) {
$scope.retLink = output;
console.log($scope.retLink);
alert('inside menulinks');
});
alert('outside menulinks');
window.location.href = $scope.retLink;
//window.open("http://www.stackoverflow.com/");
}
else
{
window.open($scope.model.ComponentModel.Screen.urlwebpage);
}
}
although the $scope.retLink can be logged on console.log but when i placed it inside the window.open($scope.retLink); it is not triggering.
Suggestions and inputs are greatly appreciated.
Thanks!

My setInterval function isn't working at all, Google Chrome

I am a fairly new programmer, and currently am trying to understand OOP from the JS side of things. I have some pretty basic code for a flashing cursor, yet for some reason it isnt working. The page loads, and the cursor just appears onscreen with no changing. The code is below:
<html>
<head>
<title>Cursor</title>
<script src="jquery.js"></script>
<script>
var str = null;
var counter = 0;
var flipFlop = function() {
alert("working");
if(counter === 0) {
document.getElementbyId('console').style.visibility='visible';
counter = 1;
}
else if(counter === 1) {
document.getElementbyId('console').style.visibility='hidden';
counter = 0;
}
else {
//debug alert
alert("function broken.");
}
};
var setIntOnload = function() {
setInterval(function() {
flipFlop();
}, 1000);
};
</script>
</head>
<body onload="setIntOnload()">
<div id="console">
|
</div>
</body>
</html>
Not sure why this isnt working... Help would be appreciated :)
PS First Post :D
Its Working
Change getElementById instead of getElementbyId
Fiddle
You have a typo in your code. Use getElementById instead of getElementbyId. JavaScript variable names (and methods) are case sensitive.
Fiddle: http://jsfiddle.net/FcrQ7/
Always check your browser console in case something in your code is not working. You had the following error:
Uncaught TypeError: Object # has no method 'getElementbyId'

"Unable to load http://url status:0" error in onbeforeunload-method

may someone of you can help me to find this problem?
I've got an xpage with client-side js-code included which should be executed when you decide to leave the page. In the client-side js you refer to a button and click it automatically. This button got some server-side js code included and change the flag from a document from ("opened by ..." to "").
The thing is that somehow the client-side js did not work in all different browsers except the current IE (10.0.5) and throws the error:
unable to load http://urlofthedocument/... status:0
The funny thing about this is, when I insert an alert()-method right after the click()-method everything works fine in every browser. But as I don't want to include this alert statement I figure out there must be something different to avoid this. (A short pause instead of the alert-method also did not work.)
My CS JS-Code:
window.onbeforeunload = WarnEditMode;
function WarnEditMode(){
if(needUnloadConfirm == true){
var el = window.document.getElementById("#{id:Hidden4SSJS}");
el.click();
//document.open();
//document.write(el);
//document.close();
//alert("You're about to leave the page");
//pause(5000);
}
}
function pause(millis){
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis)
}
This refers to to button, which executes following SS JS code, after it is clicked:
try{
print("Hidden4SSJS-Button-Test # Person");
var db:NotesDatabase = database;
var agt:NotesAgent;
var doc:NotesDocument = XPPersonDoc.getDocument()
agt = db.getAgent("(XPUnlockDocument)");
agt.run(doc.getNoteID());
}catch(e){
_dump(e);
}
May you guys can help me with this?
I would do this using the XSP object with a hidden computed field (and not your special button)...
Something like this:
function WarnEditMode(){
if(needUnloadConfirm == true){
XSP.partialRefreshGet("#{id:unlockDocCF1}", {
params: {
'$$xspsubmitvalue': 'needToUnlock'
},
onComplete: function () {
alert('You are about to leave this page and the document has been unlocked.');
},
onError : function (e) {
alert('You are about to leave this page and the document has NOT been unlocked.\n' + e);
}
);
}
pause(5000);
}
Then the computed field's javascript would be something like this:
try{
var sval = #Explode(context.getSubmittedValue(), ',');
if (sval == null) return result + " no action.";
if (!"needToUnlock".equals(sval[0])) return result + " no action.";
print("Hidden4SSJS-Button-Test # Person");
var db:NotesDatabase = database;
var agt:NotesAgent;
var doc:NotesDocument = XPPersonDoc.getDocument()
agt = db.getAgent("(XPUnlockDocument)");
agt.run(doc.getNoteID());
return 'document unlocked.';
}catch(e){
_dump(e);
}

Categories

Resources