I have an old web app developed in ASP.Net with the old type code behind style. I am eventually going to re-do the complete web app in ASP.Net MVC 3 - just starting to complete the first sections and integrate them. I am having some problems with a menu that appears along the top. So there is a home button it looks like the below in code:
onclick='document.location.href=\"Home.aspx\"'>
However I am getting the url in the new mvc part of web were I have the menu and i try to hit the home icon I get a page not found:
/MyWeb.Web.App/mvc/Controller/Home.aspx
The actual home aspx page exists in the location \MyWeb.Web.App\mvc\Home.aspx
I am currently stumped as to what I can edit in the JS href to get it to point to this location so the icons will work in the existing implementation and the new MVC one.
Thanks.
Use urls helpers. For example if you are using Razor and this code is inside an MVC view:
onclick='document.location.href="#Url.Content("~/mvc/home.aspx")"'>
or if you are using the WebForms view engine:
onclick='document.location.href="<%= Url.Content("~/mvc/home.aspx") %>"'>
UPDATE:
If this is in a separate javascript file then you could declare a global variable inside your Razor view that will point to the base url:
<script type="text/javascript">
var baseUrl = '#Url.Content("~/mvc")';
</script>
and then inside your javascript file:
onclick='document.location.href=\"" + baseUrl + "/home.aspx\"'
There are of course much better ways to achieve that but since you haven't provided enough details about your scenario it's difficult to suggest what might be best. For example you could use HTML5 data-* attributes in your DOM or some other elements to generate properly this url and access it in your javascript file.
Related
So, I'm using jsfiddle to follow THIS
{{respondedText}}
<div>{{respondedText}}</div>
However, say I want to read in HTML content from a file or site and then load it into that div; instead of displaying "Event Received: Event 2".
This is ultimately a building block for me in what I'm trying to use it for. I'm hoping, by successfully getting this example to work, that I can build a webapp that has buttons that, onpress, will load html from another local file on my server without reloading the entire page.
To fill an element with active HTML you have to use the v-html directive
<div v-html="respondedTest"></div>
This will allow any valid HTML but you have to note that you can't load Vue components asynchronously this way; It's only for static HTML.
Here is your JSFiddle Updated to send some HTML with the click events.
EDIT:
Looking into the spirit of your question you might want to look at vue-router It's a pretty good system to allow you to have a single page app with a routing system similar to a standard page routing system. It also allows you to mount Vue components in your pages instead of static HTML.
I'm pretty new to web development. What is the best practice in keeping the same sidebar and other elements across web pages on one's site? Do you store the sidebar html and call that? If so, how would one go about doing something like that?
There're many options to handle this problem but I've found easy one using jQuery. Use this if it suits your requirements.
Add the jQuery CDN in your HTML file.
Create a JS file as sidebar.js.
Copy all your HTML code of the sidebar and store as a string variable in a function of the JS file. as
function loadNavbarDiv() {
String navbar_code_str = '<nav><div>...</div></nav>
$('body').append(navbar_code_str);
}
Then in the HTML file, you want to add navigation bar, add folowing code in your <head>
<script src="sidebar.js"></script>
<script>
$(document).ready(function(){
loadNavDiv();
});
</script>
It's working fine for me.
Happy coding!
Here's one way to do it: use "include" files. No JavaScript required. The server does the work, instead of requiring the client to add the content.
SSI, or Server Side Includes, were first developed to allow Web
developers to "include" HTML documents inside other pages. If your Web
server supports SSI, it's easy to create templates for your Web site.
Save the HTML for the common elements of your site as separate files.
For example, your navigation section might be saved as navigation.html
or navigation.ssi.
Use the following SSI tag to include that HTML in each page.
<!--#include virtual="path to file/include-file.html" -->
Use that same code on every page that you want to include the file.
That page also describes some other approaches. But if you know this is called using include files, you can search for it more easily. For example, this article describes includes and how to call them from JavaScript if you must.
As long as you're only coding in html, you will need to copy your html into every page. You can store the css for the sidebar in one and the same file and call that on every page though.
Other scripting languages and frameworks might contain templates (php) or master pages (asp.net) for example which make it possible to use the same code in different pages.
I am attempting to create a plug and play shopping cart in Rails 3.1 that allows users to add a shopping cart to their site by just adding a link to a javascript file. The items for sale are input on my end and stored in this js file and rendered with jquery templates. I currently have an action that renders the corresponding js, but I was wondering if there was a way to create a new minified js file for each site and link to this file in each site instead of the show action that renders the js.
For example, for store#1, I would like to create and save a js file called store1.js and serve that file instead of calling the show.js action that creates the javascipt array for the jquery templates every time.
You could try using action caching to have it only render the action once. Then you can utilize cache sweepers to invalidate the cached js when you make any updates that would change the information in the js.
I think that really might be your best option. Your going to go through a lot more trouble trying to get precompiled dynamic JS like that, especially if the content has a tendency to change at all.
and in JavaScript
document.getElementById('myId');
This is not working in JSF application. However, the same is working if I save the generated HTML in my system and open it .
Any Help ?
When writing JavaScript code for a component based MVC framework which generates HTML, like JSF, you should not focus on the source code of the component based MVC framework, but on its generated HTML output.
If you can't tell this beforehand based on the source code, then you need to just open up the page in your favourite webbrowser and then rightclick the page and choose View Source. You'll see that the generated Client ID's are prepended by the ID's of the UINamingContainer components (like h:form, h:dataTable and f:subView). If you don't specify an ID for each of them, you will get an autogenerated ID like j_id_xxxx. To ease the work, you need to specify an ID for them. E.g.
<h:form id="form">
Also see this blog article for more information and hints. This blog article may also be useful to learn more about the wall between Java/JSP/JSF and JavaScript.
I've a couple of extension methods I've been developing for a couple of projects, they currently rely heavily on some AJAX to make bits and pieces work. The problem is that they require copying and pasting JavaScript files to the project you want to use it in.
As this JavaScript file only needs to be used once (all instances of the rendered control use the same file) I'd like to do something like add the script element to the headers collection of the page it's used on via a web-resource (embedding the file as a resource in the assembly). In Web-forms this wasn't a problem - you could add a script block to the headers with a specific ID and simply check for it on page load.
What's the MVC equivalent - is there an equivalent?
I'd like a solution that doesn't require the consumer to copy and paste/ add lines to pages or config...any thoughts?
Stephen Walther has some very good articles on MVC, including Html Helpers.
http://weblogs.asp.net/stephenwalther.
A great place to see Html Helpler code is the MVC source code available at
Codeplex.
There is a tutorial at www.asp.net/mvc on Html Helpers
Here ya go, this guy wrote a custom FormlessScriptManager that will let you register scripts even when there is no <form runat="server"> in your page.
http://developmentalmadness.blogspot.com/2008/04/abstracting-systemwebuiscriptmanager.html