If statement does nothing javascript - javascript

Okay so here's a simple question that I understood but got asked... let's say you have an if statement
if (window.location.href == 'http://' ||
window.location.href == 'http://?pg=pgOldMainMenu' ||
window.location.href == 'http://default.asp#')
else {}
How can you say "if positive do nothing, else load this HTML"?

Simply like this
var href = window.location.href;
if (href == 'http://' ||
href == 'http://?pg=pgOldMainMenu' ||
href == 'http://default.asp#') {} // do nothing
else {
href = 'url you want to load';
}

Related

Javascript add multiple if - window.location.hostname

I have 5 websites using the same DB, and I need to add an global .js to check window.location.hostname and redirect if dont match one of my domains:
if (window.location.hostname != 'mysite1.com')
if (window.location.hostname != 'mysite2.com')
if (window.location.hostname != 'mysite3.com')
if (window.location.hostname != 'mysite4.com')
if (window.location.hostname != 'mysite5.com')
{
window.location.href = 'https://google.com';
}
The js will be inserted in the DB, and will display in all my sites.
HOW to add multiple instances of IF...window.location.hostname ... in the RIGHT WAY
I don't know if I understood your question. If you want to merge those 'if' conditions you can do like this:
if(!['mysite1.com','mysite2.com','mysite3.com','mysite4.com','mysite5.com'].includes(window.location.hostname)){
window.location.href = 'https://google.com';
}
Change your code and add AND as follows:
if (window.location.hostname != 'mysite1.com'
&& window.location.hostname != 'mysite2.com'
&& window.location.hostname != 'mysite3.com'
&& window.location.hostname != 'mysite4.com'
&& window.location.hostname != 'mysite5.com')
{
window.location.href = 'https://google.com';
}
I would define a list (array) of all domains first and then check if the current domain is on the list
listOfHostnames = ['mysite1.com', 'mysite2.com', 'mysite3.com'];
if (!~window.location.hostname.indexOf(listOfHostnames)) {
window.location.href = 'https://google.com';
}

Check browser and language

I have a code (listed below) that checks if your browser language is set to "de". If your browser is "de" it will send you to the adress "de.html". If it's not "de" you will be sent to "en.html". This works fine but here is the problem:
This is for a Webflow website and they have a editor witch is url/?edit. The code still try to check your language when going to that adress. It just prints out url/?editenenenenenenenenen.. and so on. Is there a way to check if the user is on url/?edit and if so, do nothing?
var lang = window.navigator.language;
var userLang = window.navigator.userLanguage;
if (lang == "de" || userLang == "de") {
window.location.href = window.location.href + "de.html";
}
else {
window.location.href = window.location.href + "en.html";
}
Wrap another if statement around your current if like this:
if (!window.location.href.includes('/?edit')) { ... }
This will check if the user is on url/?edit and if so, does nothing.
As a whole:
var lang = window.navigator.language;
var userLang = window.navigator.userLanguage;
if (!window.location.href.includes('/?edit')) {
if (lang == "de" || userLang == "de") {
window.location.href = window.location.href + "de.html";
} else {
window.location.href = window.location.href + "en.html";
}
}
If the URL is www.somedomain.com/editor?edit, then you can get the ?edit part using :
window.location.search // '?edit'
Using that :
if(window.location.search === '?edit') return;
or more loosely :
if(window.location.search.includes('?edit')) return;

birt reports JavaScript conditional expression

I'm trying to get a formula scripted properly, can some one help me please. I'm using Birt 3.7.1. thanks. This is for a Maximo report
if(row["special"] == 'W' && row["metername"] == null){
false} *** I need this coded --don't hide --> must have a task ***
else{
true}
if(row["special"] == 'W' && row["metername"] != null){
false} *** I need this coded --don't hide --> must have task --> view ***
else if(row["special"] == 'W' true
else true}
I'm not entirely sure what you're asking here, but if you're looking to just get your code here formatted validly, here you go:
if (row["special"] == 'W' && row["metername"] == null) {
return false;
} else {
return true;
}
if (row["special"] == 'W' && row["metername"] != null) {
return false;
} else if (row["special"] == 'W') {
return true;
} else {
return true;
}
I'm not sure its the most practical code, but there you go.

Javascript error Object function has no method 'url'

I was looking at using syte as a blog. I've set it up, but when I try to use the side navigation buttons I get this javascript error:
Object function (a,b){return new e.fn.init(a,b,h)} has no method 'url' [http://127.0.0.1:8000/static/js/components/links.js:15]
Here is links.js:
var $url;
function setupLinks() {
$('a').click(function(e) {
if (e.which == 2)
return;
e.preventDefault();
e.stopPropagation();
if (this.href == $url)
return;
var url = $.url(this.href.replace('/#!', ''));
$url = this.href;
if (this.id == 'home-link' && window.location.pathname == '/') {
$('#github-profile').remove();
$('#dribbble-profile').remove();
$('#twitter-profile').remove();
$('#instagram-profile').remove();
$('#lastfm-profile').remove();
$('.modal-backdrop').remove();
adjustSelection('home-link');
}
else if(this.id == 'instagram-link' && instagram_integration_enabled) {
$('#github-profile').remove();
$('#dribbble-profile').remove();
$('#twitter-profile').remove();
$('#lastfm-profile').remove();
$('.modal-backdrop').remove();
adjustSelection('instagram-link');
setupInstagram(this);
}
else if (twitter_integration_enabled && (url.attr('host') == 'twitter.com' || url.attr('host') == 'www.twitter.com')) {
$('#github-profile').remove();
$('#dribbble-profile').remove();
$('#instagram-profile').remove();
$('#lastfm-profile').remove();
$('.modal-backdrop').remove();
adjustSelection('twitter-link');
setupTwitter(url, this);
}
else if (github_integration_enabled && (url.attr('host') == 'github.com' || url.attr('host') == 'www.github.com')) {
$('#twitter-profile').remove();
$('#dribbble-profile').remove();
$('#instagram-profile').remove();
$('#lastfm-profile').remove();
$('.modal-backdrop').remove();
adjustSelection('github-link');
setupGithub(url, this);
}
else if (dribbble_integration_enabled && (url.attr('host') == 'dribbble.com' || url.attr('host') == 'www.dribbble.com')) {
$('#twitter-profile').remove();
$('#github-profile').remove();
$('#instagram-profile').remove();
$('#lastfm-profile').remove();
$('.modal-backdrop').remove();
adjustSelection('dribbble-link');
setupDribbble(url, this);
}
else if (lastfm_integration_enabled && (url.attr('host') == 'lastfm.com' || url.attr('host') == 'www.lastfm.com')) {
$('#twitter-profile').remove();
$('#github-profile').remove();
$('#dribbble-profile').remove();
$('#instagram-profile').remove();
$('.modal-backdrop').remove();
adjustSelection('lastfm-link');
setupLastfm(url, this);
}
else {
window.location = this.href;
}
});
}
function adjustSelection(el) {
$('.main-nav').children('li').removeClass('sel');
$('#' + el).parent().addClass('sel');
if (el == 'home-link')
$url = null;
}
This looks to be an issue with a missing javascript library. The Syte installation instructions require you to run compress.py before deploying the site. It's supposed to autoimport and compress the javascript libraries, yet it doesn't seem to be doing that. Here is compress.py:
import os
import sys
import subprocess
import shlex
import traceback
path_to_here = os.path.abspath(os.path.dirname(__file__))
path_before_site = path_to_here[0:path_to_here.rfind('syte')]
sys.path.append(path_before_site)
os.environ['DJANGO_SETTINGS_MODULE'] = 'syte.settings'
from django.conf import settings
def compress_statics():
try:
#This won't work on windows.
subprocess.check_call(shlex.split('mkdir -p static/css static/js/min'))
except Exception:
print 'Make sure to create "syte > static > css" and "syte > static > js > min" before compressing statics.'
compress_styles()
compress_js()
def compress_styles():
less_path = 'static/less/styles.less'
css_path = 'static/css/'
try:
subprocess.check_call(shlex.split('lessc {0} {1}styles-{2}.min.css -yui-compress'
.format(less_path, css_path, settings.COMPRESS_REVISION_NUMBER)))
print 'CSS Styles Generated: styles-{0}.min.css'.format(settings.COMPRESS_REVISION_NUMBER)
except Exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
stack_trace = traceback.format_exception(exc_type, exc_value, exc_traceback)
print stack_trace
def compress_js():
js_files = [
'libs/jquery.url.js',
'libs/require.js',
'libs/handlebars.js',
'libs/moment.min.js',
'libs/bootstrap-modal.js',
'libs/spin.min.js',
'libs/prettify.js',
'components/base.js',
'components/mobile.js',
'components/blog-posts.js',
'components/links.js',
]
if settings.TWITTER_INTEGRATION_ENABLED:
js_files.append('components/twitter.js')
if settings.GITHUB_INTEGRATION_ENABLED:
js_files.append('components/github.js')
if settings.DRIBBBLE_INTEGRATION_ENABLED:
js_files.append('components/dribbble.js')
if settings.INSTAGRAM_INTEGRATION_ENABLED:
js_files.append('components/instagram.js')
if settings.DISQUS_INTEGRATION_ENABLED:
js_files.append('components/disqus.js')
if settings.LASTFM_INTEGRATION_ENABLED:
js_files.append('components/lastfm.js')
combined = ''
for js in js_files:
f = open('static/js/' + js, 'r')
combined += f.read()
f.close()
f = open('static/js/combined.js', 'w')
f.write(combined)
f.close()
try:
subprocess.check_call(shlex.split('uglifyjs -o static/js/min/scripts-{0}.min.js static/js/combined.js'.format(settings.COMPRESS_REVISION_NUMBER)))
subprocess.check_call(shlex.split('rm -f static/js/combined.js'))
print 'JavaScript Combined and Minified: scripts-{0}.min.js'.format(settings.COMPRESS_REVISION_NUMBER)
except Exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
stack_trace = traceback.format_exception(exc_type, exc_value, exc_traceback)
print stack_trace
if __name__ == "__main__":
compress_statics()
sys.exit()
My javascript skills are lacking so any help would be much appreciated!
You are not including the dependancies on your page most likely, specifically jquery.url.js
Include all the listed dependancies and it should work.
var url = $.url(this.href.replace('/#!', ''));
$.url does not exist
The issue was that I had embedded javascript in one of my tumblr posts that was interfering. Removed the javascript and now it's fixed.

multiple links in if statement

I have a script that specifices that a certain element does not be shown when on a specific page. Below is the code I used:
<script type="text/javascript">
function callOnPageLoad()
{
var url = window.location.href;
if(url == "http://www.exampledomain.com")
{
document.getElementById('rolex').style.display = 'none';
}
}
</script>
However I need to put a few more url's in the if statement, what is the right way of doing this?
Many thanks
I preffer to generate associative array and then check if string is set. It can be obtained from AJAX, from different script etc. and isn't hradcoded into if
<script type="text/javascript">
var urlArray = { "http://www.exampledomain.com" : true, "http://www.exampledomain.com/foobar.html" : true };
function callOnPageLoad()
{
var url = window.location.href;
if( urlArray[url] )
{
document.getElementById('rolex').style.display = 'none';
}
}
</script>
if(url == "http://www.exampledomain.com" || url == "http://www.anotherdomain.com")
{
}
Add more conditions in the if block:
<script type="text/javascript">
function callOnPageLoad()
{
var url = window.location.href;
if(url == "http://www.exampledomain.com" || url == "anotherurl" || url == "andanother")
{
document.getElementById('rolex').style.display = 'none';
}
}
</script>
have an array of urls and iterate
function callOnPageLoad()
{
var urls = [
"http://www.exampledomain.com",
"http://www.exampledomain2.com"
];
var url = window.location.href;
for ( var i=0; i < urls.length; i++ ) {
if(url == urls[i])
{
document.getElementById('rolex').style.display = 'none';
break;
}
}
}
you can create an array of those urls and run for loop thought them, this will be more dynamic approach to your problem.
using long if statements is not advisible because you can loose a character here or a bit of logic there
If your urls point to external urls or match other patterns that you can distinguish them from other urls you can use it without an array.
function callOnPageLoad(type)
{
var url = window.location.href;
var urls=new array("http://www.exampledomain1com","http://www.exampledomain2.com");
if(url in urls){
document.getElementById('rolex').style.display = 'none';
}
}
if(url == "http://www.exampledomain.com" || url =="http://www.url2.com" || url == "http://www.url3.com") and so forth ... ?

Categories

Resources