Why isn't my JavaScript rendering properly when using Blazor? - javascript

So I have this theme that I usually use, it's a folder containing all the files, such as the assets, the html pages etc etc.
And if I start the index page from there and I click a menu item, wen can clearly see that the animation works perfectly.
However, as soon as I add the same CSS and JS which that page uses, this is how it starts acting.
You can't see it but I'm clicking each item, and it doesn't animate the menu.
It's also missing some icons which you can see if you compare the two.
Now here is what's interesting..
It works PERFECTLY if I change the render-mode to static rather than ServerPrerendered here
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
But if I do that we have this issue..
Rendering server components from a static HTML page isn't supported.
The way I add the HTML is by doing this.. I go into the _Host.cshtml file and at the top in the tags like so
<!DOCTYPE html>
<html lang="en">
<head>
...
<link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css" />
<link href="assets/css/site.css" rel="stylesheet" />
<!-- Articles CSS -->
<link type="text/css" rel="stylesheet" href="assets/plugins/dataTable/datatables.min.css">
<link type="text/css" rel="stylesheet" href="assets/plugins/summernote/summernote.min.css">
<link type="text/css" rel="stylesheet" href="assets/plugins/bootstrap-tagsinput/bootstrap-tagsinput.css">
<!-- Main CSS -->
<link type="text/css" rel="stylesheet" href="assets/css/style.css" />
And the same goes for the JS files, but I do it before the tag like so..
<script src="_framework/blazor.server.js"></script>
<!-- Footer Script -->
<!--================================-->
<script src="assets/plugins/jquery/jquery.min.js"></script>
<script src="assets/plugins/jquery-ui/jquery-ui.js"></script>
<script src="assets/plugins/popper/popper.js"></script>
And it can obviously find some of it because it keeps the same general style, and when I render it using "static" as I mentioned before, it works just fine. It's almost as if it's rendering something too fast.
Where did I go wrong on this?

I think that's now easier to achieve with the Net5. You can add this JavaScript module to your .NET library as a static web asset (wwwroot/exampleJsInterop.js) and then import the module into your .NET code using the IJSRuntime service:
await jsRuntime.InvokeAsync<JSObjectReference>("import", "./_content/MyComponents/exampleJsInterop.js");
The “import” identifier is a special identifier used specifically for importing a JavaScript module. You specify the module using its stable static web asset path: _content/[LIBRARY NAME]/[PATH UNDER WWWROOT].
Learn more about it here

Related

Adding JS from HTML Template in Sveltekit project

I have an HTML Template with CSS and JS. I wanted to import it in SvelteKit.
Adding HTML and CSS is pretty straightforward. When it comes to JS, there are problems.
As far as I have tested with the browser, The imported JS code works, but it fails to affect the DOM.
Ex. console.log works, but adding bootstrap.js or slick.js do not work. Also, custom JS written is not loading. This means there is no issue with the directory.
This is how I have imported CSS and JS in Svelte.
<svelte:head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<link rel="stylesheet" href="/css/style.css" />
<link rel="stylesheet" href="/css/responsive.css" />
<script src="/js/jquery-3.6.0.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/owl.carousel.min.js"></script>
<script src="/js/isotope.pkgd.min.js"></script>
<script src="/js/slick.min.js"></script>
<script src="/js/script.js"></script>
</svelte:head>
Is there another way to import external JS to Svelte files, a way where the JS will affect the DOM?
For global scripts, you can add them to your app.html or index.html in your SvelteKit project, just like how you would with a normal HTML webpage:
src/app.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Metadata such as title or description -->
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width" />
<!-- Your CSS / JS globally -->
<link rel="stylesheet" href="https://some-cdn.com/css/bootstrap.min.css" />
<script src="https://some-cdn.com/js/script.js"></script>
%sveltekit.head%
</head>
<body>
<div>%sveltekit.body%</div>
</body>
</html>
HOWEVER, it seems you're importing them from an absolute URL. If this is located inside the /lib folder, you can import them using a tag in your +layout.svelte:
<script>
import "$lib/css/bootstrap.min.css"
// you can do the same with JavaScript
</script>
A very common scenario, however, is that they are in node_modules. To import these, you can import them from the module:
<script>
import "module/style.css"
</script>
If they are in your static folder, you can use app.html to import them as shown in the first example.
Since you're using bootstrap, I would recommend to use svelte-add/bootstrap and import any necessary JS in app.html using a CDN

How to fix bootstrap path not working when I put it inside a folder

It is my first time using bootstrap 3, what I want to do is to make a header so that I will just call the file whenever I will create a new page but the problem is that when I created a folder and inside is index.php when I put the code from the original file it didn't work.
Here is the image of what I did
Here is the image of the original
Here is the code in html (only the link and script)
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
<link
href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
rel="stylesheet">
<!-- Custom styles for this template-->
<link href="css/sb-admin-2.min.css" rel="stylesheet">
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Custom scripts for all pages-->
<script src="js/sb-admin-2.min.js"></script>
<!-- Page level plugins -->
<script src="vendor/chart.js/Chart.min.js"></script>
<!-- Page level custom scripts -->
<script src="js/demo/chart-area-demo.js"></script>
<script src="js/demo/chart-pie-demo.js"></script>
I don't clearly get it, I mean I accessed the file path and try to recode it but still it won't show the design and functions in jquery, I hope someone can explain me since I am new to this.
add / to all src or href. it's different between /vendor and vendor.
you need to access file at root but not page/vendor

LESS.js Not Loading?

I'm attempting to use LESS but even after following along with tutorials, the page fails to render. Previously I had a few dozen different classes in the body class="" tag, but for the sake of cleanliness, I want to use LESS to encapsulate it in a single class.
It hasn't loaded in Chrome, Safari, or Firefox
All of the classes I use within the .LESS file are included in the .CSS
index.html:
<head>
<link rel="stylesheet" type="text/css" href="./index_files/site.css">
<link rel="stylesheet/less" type="text/css" href="./index_files/site.less"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/less.js/3.0.0/less.min.js">
</script>
</head>
<body class="main_body">
...
</body>
site.LESS:
#import (less) "site.css";
main_body {
.site-spacing-custom;
.site-outer-padding-large;
.site-alignment-center;
.tweak-site-ajax-loading-enable;
.ancillary-header-full-width;
.tweak-header-cart-style-text;
.show-site-footer;
}
After hosting the page via my laptop, it's now rendering but the .LESS code isn't working at all:

Meteor: Dynamic page not transitioning properly

Okay, this is a really weird one. Unfortunately, I can't show a whole lot of code because I really have no idea where the problem is originating -- However, I can describe the problem. That said, if there' s some code you think is relevant, please ask and I'll post it! So, here goes:
I have an HTML template (found here) that I'm chopping up into Blaze templates for integration into a real estate site that I'm working on using Meteor. The template includes all these external CSS and JavaScript files that I've included by simply leaving the templates as-is and putting all the assets required by the template into /public folder of the Meteor project. The files are loaded via regular <script> and <link> elements embedded in the <head> and near the bottom of the main layout.
Here is what my <head> looks like:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Real Places</title>
<link rel="icon" href="images/cropped-ms-icon-310x310-32x32.png" sizes="32x32" />
<link rel="icon" href="images/cropped-ms-icon-310x310-192x192.png" sizes="192x192" />
<link rel="apple-touch-icon-precomposed" href="images/cropped-ms-icon-310x310-180x180.png">
<!-- Google font-->
<link href='http://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic,500,500italic,700,700italic,900' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Dosis:400,700,600,500' rel='stylesheet' type='text/css'>
<!-- Include the flexslider stylesheet -->
<link href="js/flexslider/flexslider.css" rel="stylesheet">
<!-- Include the lightslider stylesheet -->
<link href="js/lightslider/css/lightslider.min.css" rel="stylesheet">
<!-- Include the owl-carousel stylesheet -->
<link href="js/owl.carousel/owl.carousel.css" rel="stylesheet">
<!-- Include the swipebox stylesheet -->
<link href="js/swipebox/css/swipebox.min.css" rel="stylesheet">
<!-- Include the select2 stylesheet -->
<link href="js/select2/select2.css" rel="stylesheet">
<!-- Include the font-awesome stylesheet -->
<link href="css/font-awesome.min.css" rel="stylesheet">
<!-- Include the select2 stylesheet -->
<link href="css/animate.css" rel="stylesheet">
<link href="js/magnific-popup/magnific-popup.css" rel="stylesheet">
<!-- Main stylesheet -->
<link rel="stylesheet" href="css/main.css"/>
<!-- Default Theme stylesheet -->
<link rel="stylesheet" href="css/theme.css"/>
</head>
Here is what my main layout looks like:
<template name="app_home"> <!-- note this is distinct from site_home, which is a different template -->
<body class="inspiry-slider-two">
{{> page_loader}}
{{> mobile_header}}
{{> site_header}}
{{> Template.dynamic template=main}}
{{> site_footer}}
{{> login_modal}}
{{> real_places_includes}}
</body>
</template>
All of the scripts that the template depends on are inserted via the real_places_includes template call. Here is what this template looks like:
<template name="real_places_includes">
<script src="js/jquery-1.12.3.min.js"></script>
<script src="js/flexslider/jquery.flexslider-min.js"></script>
<script src="js/lightslider/js/lightslider.min.js"></script>
<script src="js/select2/select2.min.js"></script>
<script src="js/owl.carousel/owl.carousel.min.js"></script>
<script src="js/swipebox/js/jquery.swipebox.min.js"></script>
<script src="js/jquery.hoverIntent.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script src="js/jquery.form.js"></script>
<script src="js/transition.js"></script>
<script src="js/jquery.appear.js"></script>
<script src="js/modal.js"></script>
<script src="js/meanmenu/jquery.meanmenu.min.js"></script>
<script src="js/jquery.placeholder.min.js"></script>
<script src="js/custom.js"></script>
<!-- .modal -->
<script>
...
// This is a bunch of modal code specific to the template
...
</script>
</template>
This works great when the page first loads, but when I route away from the main page and back, it appears as if assets aren't loading properly when I come back. Here is some relevant routing code:
FlowRouter.route('/', {
name: 'Site.home',
action() {
BlazeLayout.render('app_home', { main: 'site_home' });
}
});
FlowRouter.notFound = {
action() {
BlazeLayout.render('app_home', { main: 'site_not_found' });
}
};
Right now almost all the links in the template don't work since I haven't swapped them out for 'live' {{pathFor 'route'}} calls. So, if I say, click on one of those links, I get the 404 page, as I should. It appears to load just fine. On the 404 page, I added a {{pathFor 'Site.home'}} to navigate the user back to the main page. However, when you click it, all the page elements on the home page (the ones that previously loaded fine!) that depend on JavaScript don't seem to format correctly. I checked the Chrome debugger to find this out. However, all of the assets are still loaded properly according to Chrome's Resource inspector!
Here is what the page looks like initially and after routing back home:
Before routing
After routing
As you can see, the select boxes haven't been rendered properly, the slider at the top of the page below the nav bar also isn't rendering and displays an animated loading graphic... basically anything that depends on JavaScript is kaput.
Here are a couple of screenshots of my debugger highlighting the problem:
Before routing
After routing
This is what that same 'Location' select looks like before and after using my route.
So, for whatever reason, when I hit some route, the code that the route depends on doesn't seem to execute as it should in order to format the page as it was originally HOWEVER if the route is hit when the site first loads, it executes fine... I feel like I need to be firing some global event or something like that manually, but I don't really know what... Or maybe something else entirely. I have tried firing 'load' and 'ready' events manually on the Template.site_home.onRendered callback, but that hasn't worked.
Does anyone have any idea how I can fix this and make the page transition properly?

Adding bootstrap in HTML

I'm going to use bootstrap to build up my front end. Also I'm using JSP and JSTL
What I've read in some articles over the internet is the proper approach of adding an external javascript file should be written before the body closing tag for more optimization of the page. Now I'm trying to apply this with the bootstrap, what i did is put the css of the bootstrap to an external html file:
import_header.html:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="bootstrap-css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="bootstrap-css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="bootstrap-css/bootstrap-responsive.css">
<link rel="stylesheet" type="text/css" href="bootstrap-css/bootstrap-responsive.min.css">
which I imported to the head of the html like this:
<jsp:include page="import_header.html" flush="true" />
Now I want to do the same thing with the js file of the of the bootstrap and the jQuery lib but I don't know what is the proper way of doing this.
that's the recommended way, how to place them:
e.g. footer.html
<!-- Placed at the end of the document so the pages load faster -->
<script src="jquery/jquery-2.0.3.min.js"></script>
<script src="bootstrap/bootstrap-2.3.1.min.js"></script>
<script src="myOwn.js"></script>
</body>
</html>

Categories