In order to get a mobile nav menu working I've got this Javascript block:
function AddMobileNavMenu() {
var $body = $('body')
$(
'<div id="navPanel">' +
$('#nav').html() +
'' +
'</div>'
)
.appendTo($body)
.panel({
delay: 500,
hideOnClick: true,
hideOnSwipe: true,
resetScroll: true,
resetForms: true,
side: 'left'
});
}
But I can't get that javascript working with Blazor - I've read through all the JSInterop documentation and figured out that this (in the _Host.cshtml file):
#page "/"
#namespace FIS2.Pages
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FIS2</title>
<base href="~/" />
<link rel="stylesheet" href="FontAwesome/scss/fontawesome.min.css" />
<link rel="stylesheet" href="FontAwesome/scss/solid.min.css" />
<link rel="stylesheet" href="FontAwesome/scss/regular.min.css" />
<link rel="stylesheet" href="css/syncfusion/syncfusionTheme.css" />
<link rel="stylesheet" href="css/site/Components/site.min.css" />
</head>
<body>
<component type="typeof(App)" render-mode="ServerPrerendered" />
<script src="_framework/blazor.server.js"></script>
<script src="Scripts/jquery-3.6.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
<script src="Scripts/AddMobileNavMenu.js"></script>
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
Reload
<a class="dismiss">🗙</a>
</div>
</body>
</html>
Allows you to call it from the relevant component, like so:
await jsRuntime.InvokeVoidAsync("AddMobileNavMenu");
So long as you've injected IJSRuntime
The only problem is that it doesn't seem to be loading in the jQuery and triggers an error saying appendTo is not a function. What am I missing in order to make this work?
Looking at the issue with fresh eyes, I've realised my mistake - it turns out that it wasn't complaining about appendTo, but rather panel. I hadn't realised that was part of another library - importing that library after jquery (but before the custom function) solved the issue.
I'll leave the question here in case someone else goes down the train of thought that I did on Friday.
Related
I am trying to add a link to .js file in Pages/_Layout.cshtml However I am getting following exception:
Severity Code Description Project File Line Suppression State Error
(active) CS0103 The name 'antv' does not exist in the current
context Blazing C:\Users\Laptop\source\repos\Blazing\Blazing\Pages_Layout.cshtml 31
_Layout.cshtml:
#using Microsoft.AspNetCore.Components.Web
#namespace BlazingDiary.Pages
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="BlazingDiary.styles.css" rel="stylesheet" />
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head>
<body>
#RenderBody()
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
Reload
<a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.server.js"></script>
<script src="https://unpkg.com/#antv/g2plot#1.1.28/dist/g2plot.js"></script>
<script src="_content/AntDesign.Charts/ant-design-charts-blazor.js"></script>
<link href="_content/AntDesign/css/ant-design-blazor.css" rel="stylesheet" />
<script src="_content/AntDesign/js/ant-design-blazor.js"></script>
</body>
</html>
Problems is in this particular link https://unpkg.com/#antv/g2plot#1.1.28/dist/g2plot.js. Was googling for couple of hours without success. Any ideas?
This is solved in .net 6.
In .net 5 (and before) the razor engine appears to stick its nose where it doesn't belong. Luckily there is an easy fix, escape the # as ##:
<script src="https://unpkg.com/##antv/g2plot#1.1.28/dist/g2plot.js"></script>
I am doing up a log-in page but I'm unsure why the validation script is not loading. I was able to log in without an error like this yesterday. However, when I tried to log in today, I was met with this error.
Is it the placement of my script?
I've tried rearranging the scripts, and checking other files such as my js and the file location of jquery.validate.min.js, but all seems fine.
.html
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<!--<meta http-equiv="refresh" content="5">-->
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge" />-->
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link href="../Content/bootstrap.min.css" rel="stylesheet" />
<script src="lib/jquery.validate.min.js"></script>
<!--<link href="css/jquery.mobile-1.4.5.min.css" rel="stylesheet" />-->
<script src="scripts/common.js"></script>
.JS
var userid;
var password;
$(document).ready (function () {
$("#LoginForm").validate({ // this line is the error highlighted in console
messages: {
txtLogin: "User ID is required",
txtPassword: "Password is required",
},
I believe I should be able to log in after solving this as I have configured my PHP and other files correctly. The JS file seems fine too.
In order to use the validation method of jQuery, you need to import the relative plugin:
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
Also, at start of your JavaScript file insert this:
require('jquery-validation');
For example:
<form>
<input required>
</form>
<script src="jquery.js"></script>
<script src="jquery.validate.js"></script>
<script>
$("form").validate();
</script>
See the official documentation for more information.
I am creating a web app frontend using ReactJS.
I have downloaded a Template from W3layout which is designed using HTML5 CSS3 Custom JS file and also includes some third party/vendor JS/Jquery Plugins Like nice-select, bootstrap,slik, owl-carousel, etc
I am not a Front End Developer My expertise are in Serverside scripting & Database. So I just wanted to use the same downloaded template to develop ReactJS application.
But When page Loads/render 1st time its work fine but on navigating to other Route /Component Jquery/Other Vendor Scripts Stop Working.
Even I wanted to trigger update/change to the plugins Like
componentDidUpdate(){
// $('select').niceSelect('update');
}
All these are not working.
This is my Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="shortcut icon" href="/img/fav.png">
<meta name="description" content="">
<meta name="keywords" content="">
<meta charset="UTF-8">
<!-- Site Title -->
<title>Training</title>
<link href="https://fonts.googleapis.com/css?family=Poppins:100,200,400,300,500,600,700" rel="stylesheet">
<!-- CSS
============================================= -->
<link rel="stylesheet" href="/css/linearicons.css">
<link rel="stylesheet" href="/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/bootstrap.css">
<link rel="stylesheet" href="/css/magnific-popup.css">
<link rel="stylesheet" href="/css/nice-select.css">
<link rel="stylesheet" href="/css/animate.min.css">
<link rel="stylesheet" href="/css/owl.carousel.css">
<link rel="stylesheet" href="/css/jquery-ui.css">
<link rel="stylesheet" href="/css/main.css">
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> </head>
<body> <div id="root"></div>
<script src="/js/vendor/jquery-2.2.4.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="/js/vendor/bootstrap.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBhOdIF3Y9382fqJYt5I_sswSrEw5eihAA"></script>
<script src="/js/easing.min.js"></script>
<script src="/js/hoverIntent.js"></script>
<script src="/js/superfish.min.js"></script>
<script src="/js/jquery.ajaxchimp.min.js"></script>
<script src="/js/jquery.magnific-popup.min.js"></script>
<script src="/js/jquery.tabs.min.js"></script>
<script src="/js/jquery.nice-select.min.js"></script>
<script src="/js/owl.carousel.min.js"></script>
<script src="/js/mail-script.js"></script>
<script src="/js/main.js"></script>
How can we use the existing vendors script on our react js without any issue .
jQuery binds its listeners to the existing dom elements. When the dom changes new nodes don't get those listeners automatically. You need to call the initialization phase of each jQuery script after the navigation (and any other significant dom change). Alternatively you can make those listeners to listen to document instead:
$('button').click(function(){})
should become
$(document).on('click', 'button', function(){})
As a fast fix you can force React Router to reload the page on every navigation:
<BrowserRouter forceRefresh/>
But you are likely to get similar troubles with other react components.
Some hints to fix the issue:
try to replace jQuery plugins with react modules
use the history object provided by React Router to listen for navigation changes and reinit jQuery code
use componentDidMount and componentDidUpdate to bind and update jQuery listeners
I'm trying to implement swiping of image using html and javascript for phonegap application, following this demo PhotoSwipe there it works fine, but after adding into my project, not able to swipe the images. and even linked the js files correctly still i get this error in logcat "Uncaught TypeError: Cannot read property 'PhotoSwipe' of undefined at file:///android_asset/www/examples/01-default.html:26"
any idea on my issue? Thanks.
swipe code
<title>PhotoSwipe</title>
<meta name="author" content="Ste Brennan - Code Computerlove - http://www.codecomputerlove.com/" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link href="styles.css" type="text/css" rel="stylesheet" />
<link href="css/photoswipe.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" charset="utf-8" src="js/cordova-1.9.0.js"></script>
<script type="text/javascript">
<script type="text/javascript">
(function(window, PhotoSwipe){
document.addEventListener('DOMContentLoaded', function(){
var
options = {},
instance = PhotoSwipe.attach( window.document.querySelectorAll('#Gallery a'), options );
}, false);
}(window, window.Code.PhotoSwipe));
</script>
<script type="text/javascript" src="js/cordova-1.9.0.js"></script>
<script type="text/javascript" src="/libs/klass.min.js"></script>
<script type="text/javascript" src="js/code.photoswipe.jquery-3.0.5.min.js"> </script>
<script type="text/javascript" src="js/code.photoswipe.jquery-3.0.5.js"></script>
<script type="text/javascript" src="js/code.photoswipe-3.0.5.min.js"></script>
<script type="text/javascript" src="js/code.photoswipe-3.0.5.js"></script>
<script type="text/javascript" src="photoswiper.js"></script>
}
Add the js files before the script that you have written, it might be the cause of the problem because the photoswipe features that you are trying to use in the script have not been loaded and hence are not available yet.
Wordpress is not loading jquery - I think it may have something to do with the fact that it is loading the files absolutely, not relatively, and it won't go to the url (firebug is saying access denied to restricted uri). I have tested vanilla javascript and that works, however as soon as i try to do even the most basic function in $(document).ready(function(){}) it stops working.. is there a way to stop wordpress from appending the website uri to the linked files, and reference them relatively instead, as i think this may fix it.
The outputted head is as follows:
<!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 profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>simplespace — not quite ready yet</title>
<meta name="generator" content="WordPress 3.0.1" /> <!-- leave this for stats -->
<link rel="stylesheet" href="http://simplespace.co.nz/wp-content/themes/blass2/style.css" type="text/css" media="screen" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://simplespace.co.nz/feed/" />
<link rel="alternate" type="text/xml" title="RSS .92" href="http://simplespace.co.nz/feed/rss/" />
<link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="http://simplespace.co.nz/feed/atom/" />
<link rel="pingback" href="http://simplespace.co.nz/xmlrpc.php" />
<link rel="shortcut icon" href="http://simplespace.co.nz/wp-content/themes/blass2/favicon.ico" />
<link rel='archives' title='September 2010' href='http://simplespace.co.nz/2010/09/' />
<link rel='stylesheet' id='sociable3-css' href='http://simplespace.co.nz/wp-content/plugins/sociable-30/sociable.css?ver=5.10' type='text/css' media='all' />
<script type='text/javascript' src='http://simplespace.co.nz/wp-includes/js/jquery/jquery.js?ver=1.4.2'></script>
<script type='text/javascript' src='http://simplespace.co.nz/wp-content/plugins/wordpress-flickr-manager/js/jquery.lightbox.js?ver=3.0.1'></script>
<script type='text/javascript' src='http://simplespace.co.nz/wp-content/plugins/wordpress-flickr-manager/js/wfm-lightbox.php?ver=3.0.1'></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://simplespace.co.nz/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://simplespace.co.nz/wp-includes/wlwmanifest.xml" />
<link rel='index' title='simplespace' href='http://simplespace.co.nz/' />
<meta name="generator" content="WordPress 3.0.1" />
<script type="text/javascript" src="http://simplespace.co.nz/wp-content/plugins/audio-player/assets/audio-player.js?ver=2.0.4.1"></script>
<script type="text/javascript">AudioPlayer.setup("http://simplespace.co.nz/wp-content/plugins/audio-player/assets/player.swf?ver=2.0.4.1", {width:"400",animation:"yes",encode:"yes",initialvolume:"60",remaining:"no",noinfo:"no",buffer:"5",checkpolicy:"no",rtl:"no",bg:"dcf2fa",text:"333333",leftbg:"dcf2fa",lefticon:"333333",volslider:"666666",voltrack:"cfcfcf",rightbg:"9ee1f7",rightbghover:"dcf2fa",righticon:"333333",righticonhover:"333333",track:"FFFFFF",loader:"9ee1f7",border:"CCCCCC",tracker:"dcf2fa",skip:"666666",pagebg:"FFFFFF",transparentpagebg:"yes"});</script>
<!-- WFM INSERT LIGHTBOX FILES -->
<link rel="stylesheet" href="http://simplespace.co.nz/wp-content/plugins/wordpress-flickr-manager/css/lightbox.css" type="text/css" />
<!-- WFM END INSERT -->
<script type='text/javascript'>
$(document).ready(function() {
alert('test');
});
</script>
</head>
Wordpress puts jQuery in no-conflict mode which disabled the $() function. Unless you disable no-conflict mode in jQuery you'll have to use jQuery() instead of $().
Try this:
Full answer for those who are facing same problem
You can wrap your javascript inside a self-invoking function, then pass jQuery as an argument to it, using $ as the local variable name. For example:
(function($) {
$(document).ready(function(){
alert('test');
});
}(jQuery));
The jQuery library included with WordPress is set to the noConflict() mode. This is to prevent compatibility problems with other JavaScript libraries that WordPress can link.
In the noConflict() mode, the global $ shortcut for jQuery is not available.
I looked at the site and jquery is loading. This:
<script type='text/javascript'>
$(document).ready(function() {
alert('test');
});
</script>
wont work because WordPress uses jquery in no conflict mode. This:
jQuery(document).ready(function($){
alert('test');
});
is how it should be called in no conflict mode. Also the lightbox plugin is throwing an error:
Uncaught TypeError: Object # has no method 'lightBox'