Javascript error Object function has no method 'url' - javascript

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.

Related

Google apps script function returns undefined in html

I am building an add-on for google docs (Just for practice) that will act like email. I already incorporated sending, receiving, deleting, and viewing messages. I added the code needed for a UI modal dialog, but one of the functions is only returning undfined. I tested this function in the code.gs file, and it worked perfectly. Here is a section of code.gs:
function onInstall() {
var html = HtmlService.createHtmlOutputFromFile('Startup').setWidth(350).setHeight(170);
DocumentApp.getUi().showModalDialog(html, 'New account:');
}
function testCheck() {
var ui = DocumentApp.getUi();
ui.alert(checkUsername(ui.prompt('').getResponseText(), ui.prompt('').getResponseText()));
}
function checkUsername(un, em) {
var i; var a; var is;
var props = PropertiesService.getScriptProperties();
if (props.getProperty(un) == null) {
is = true;
} else {
return 'This username is taken!';
}
if (em.length == 0) {
return true;
} else {
var len = (em.match(/#/g) || []).length;
if (len == 1) {
if (props.getProperty(em) != null) {
return 'Someone has already registered this email address as ' + props.getProperty(em);
} else {
return true;
}
} else {
if (em.indexOf(', ') != -1) {
em = em.split(', ');
} else if (em.indexOf('; ') != -1) {
em = em.split('; ');
} else if (em.indexOf(' + ') != -1) {
em = em.split(' + ');
} else if (em.indexOf(';') != -1) {
em = em.split(';');
} else if (em.indexOf(',') != -1) {
em = em.split(',');
} else if (em.indexOf('+') != -1) {
em = em.split('+');
} else if (em.indexOf(' ') != -1) {
em = em.split(' ');
} else {
return 'Please separate your email addresses with a comma, space, or semicolon.';
}
for (i = 0; i < em.length; i++) {
a = em[i];
if (props.getProperty(a) != null) {
return 'Someone has already registered ' + a + ' as ' + props.getProperty(a);
}
}
return true;
}
}
}
Here is the html file:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
</head>
<body>
Username:<br>
<input type='text' id='user' style='width:350px' maxlength='12'/><br>
Other email addresses:<br>
<textarea id='extras' style='width:350px' rows='2'></textarea><br>
<span class='error' id='err'></span><br>
<button class='action' onClick='check()'>Next</button>
<button onclick='group()'>Groups</button><br>
<script>
function check() {
var un = document.getElementById('user').value;
var em = document.getElementById('extras').value;
var fail = document.getElementById('err');
var is = google.script.run.checkUsername(un, em);
if (typeof is == 'string') {
fail.innerHTML = is;
} else {
google.script.host.close();
google.script.run.setAccount(un, em);
}
}
function group() {
var un = document.getElementById('user').value;
var em = document.getElementById('extras').value;
var is = google.script.run.checkUsername(un, em);
if (typeof is == 'boolean') {
setGroupAddress(un, em);
} else {
document.getElementById('err').innerHtml = is;
}
}
</script>
</body>
</html>
Update: I completely retyped the functions, but the program continues to return undefined. All inputs are the correct values, and the function returns information correctly in a ui.alert() box.
I figured it out after completely reading the Google Apps Script Documentation. The google.script.run.function() API does not return a value. In order to fetch data from a script, you must have the script generate raw HTML, and create a dialog with an HTML string.
Due to security considerations, scripts cannot directly return HTML to a browser. Instead, they must sanitize it so that it cannot perform malicious actions. You can return sanitized HTML using the createHtmlOutput API
function doGet() {
return HtmlService.createHtmlOutput('<b>Hello, world!</b>');
}
The code in the HtmlOutput can include embedded JavaScript and CSS. (This is standard client-side JavaScript that manipulates the DOM, not Apps Script). All of this content is sanitized using Google Caja, which applies some limitations to your client-side code. For more information, see the guide to restrictions in HTML service.
https://developers.google.com/apps-script/reference/html/html-output#

jQuery ajax relative path not finding file

I have some script that I wrote in a HTML document but I decided to move it to an external file, so I changed the path that is in the jQuery to reflect the js file location but it doesn't seem to be able to find the PHP file it needs. Does anyone understand why this might be?
jQuery
function login(){
var u = $("#loginUsername").val;
var p = $("#loginPassword").val;
if (u === ""){
loginStatus("empty","username");
} else if (p === ""){
loginStatus("empty","password");
} else {
$("#loginbtn").css("display","none");
$("#loginStatus").html("please wait...");
var ajax = ajaxObj("POST", "../../php/login.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) === true) {
if(ajax.responseText == "login_failed"){
$("#loginStatus").html("Login failed. Please try agian");
$("#loginbtn").css("display","inline-block");
} else if (ajax.responseText == "login_failed_1"){
loginStatus("wrong","password");
} else {
window.location = "http://domain.com/"+ajax.responseText;
}
} else {
$("#loginStatus").html("fail_667389");
}
};
ajax.send("u="+u+"&p="+p);
}
}
File Hierarchy
The PHP file is in the PHP folder and the JS files is in loginScripts.
You trying to access a server file with a relative path ???
You have to use something like 'http://domain.com/php.login.php'

how to get js context from htmlparser2 directly?

how to get js context from htmlparser2 directly?
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
//console.log(text);
//console.log("JS! Hooray!");
}
},
ontext: function(text){
//fs.writeFileSync("../output/test.js",text.toString());
console.log(text);
},
onclosetag: function(tagname){
if(tagname === "script"){
//console.log("That's it?!");
}
}
}, {decodeEntities: true});
var input_src = fs.readFileSync('../input/test2.js', 'utf-8');
parser.write(input_src);
parser.end();
I try to get js from htmlparser2, but the content is not i am want.The content will include some other information expect the js. why the code do not work well?Could you give me some good ways to solve it?
You are not doing anything to limit the ontext function to only reading scripts. You need to set a flag whenever you encounter an opening script tag:
var inScriptElement = false;
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
inScriptElement = true;
//console.log(text);
//console.log("JS! Hooray!");
}
},
ontext: function(text){
if (inScriptElement) {
//fs.writeFileSync("../output/test.js",text.toString());
console.log(text);
}
},
onclosetag: function(tagname){
if(tagname === "script"){
inScriptElement = false;
//console.log("That's it?!");
}
}
}, {decodeEntities: true});

how to correct this javascript?

could you please help me to allow the asyncFileUploader to use these extensions:(rar,pdf,doc,docx,zip)...
im not an jscript expert, so i have been tying to edit the script by my self but i failed ...
var fileExtension = args.get_fileName();
if (fileExtension.indexOf('.doc') != -1) {
$get("dvFileErrorInfo").style.display = 'block';
$get("<%=lblError.ClientID%>").innerHTML = "File extension [.doc] not supported";
$get("dvFileInfo").style.display = 'none';
return;
}
Change the condition to fileExtension.indexOf('.doc') == -1 && fileExtension.indexOf('.pdf') == -1 && etc so on. Copy paste the condition and add the extensions you wanna allow. This would mean that any extensions not in the conditions will fullfil the condition and the message will display
You can use the OnClientUploadStart property on the control to fire a JavaScript function for validation, like this:
<cc1:AsyncFileUpload ID="FileUpload" runat="server"
OnClientUploadStarted="AssemblyFileUpload_Started" />
Then use this script on your page:
<script>
function AssemblyFileUpload_Started(sender, args) {
var filename = args.get_fileName();
var ext = filename.substring(filename.lastIndexOf(".") + 1);
if (ext != 'zip') {
throw {
name: "Invalid File Type",
level: "Error",
message: "Invalid File Type (Only .zip)",
htmlMessage: "Invalid File Type (Only .zip)"
}
return false;
}
return true;
}
</script>
Use other file types as well.

If statement does nothing 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';
}

Categories

Resources