javascript in masterpages ASP.NET 2.0 - javascript

I am trying to set up a masterpage that contains a javascript popup subroutine that can be used in multiple web pages. The popup already works in a single page environment. I now want to migrate it to a master page. Any ideas will be greatly appreciated. I already searched this site and tried a couple of the suggestions to no avail. W small working example would help.
Thanks
Bill

Just take what you have in your single page and place it in the master page. It is really that simple. :) Then just replace the main body of the HTML in the master page with the content sections and you should be ready to go.

One thing I did discover was that for some reason the derived page had to be in the same folder as the master page.
Most likely caused by the master page referencing a subdirectory that works for the master page, but not the internal pages themselves. One workaround would be using
ScriptManager.RegisterClientScriptInclude(string Key, string URL)
like so:
ScriptManager.RegisterClientScriptInclude("uniqueIdentifier", "~/javascript/myjs.js");
Alternatively, you can embed the file as a resource and use
ScriptManager.RegisterClientScriptResource(Page, Type, Key);
like so:
ScriptManager.RegisterClientScriptResource(this.Page, typeOf(Page), "resourceKey");

this is my current favourite, seems to be most robust and flexible. Simply include the js files in your master then create an isntance on your pages

be sure to use the scriptmanager class to add the javascript

Related

HTML sub-pages (using a domain)

I've been thinking about purchasing a domain and putting a website on it, but I'm not sure how I would add sub-pages. I'm probably using the wrong words, but I'll try to explain what I mean.
For example, the main page is stackoverflow.com. When you go to another page, it goes to stackoverflow.com/questions. Is this achieved using Javascript, or is it set up manually where you link an HTML file to the sub-page?
I'm not too sure where to start here, so any help is appreciated.
One of the simplest ways to set up a website with multiple pages is to create a folder for each page and an index.html file for each page. These can then be linked together using HTML anchors or JavaScript code. For detailed instructions on how to do this, I suggest checking out tutorials from W3Schools like how to make a website

Trouble inserting a javascript variable into an HTML tag [duplicate]

I want to add twitter card meta tags to my website. i cannot add static tags since the content attribute in the meta tag has to change dynamically.
Plz help if someone has a solution.
You can't, at least not in any way that Twitter is going to recognise.
When Twitter fetches the page, it is always going to get the meta elements that are in the HTML, never ones added with client side JavaScript.
If you are changing content dynamically then make use of the history API to update the URI, and make sure that the server will generate all the content for each URI when the URI is used as an entry point (you can still use JavaScript to generate the content when moving from another page on the site).
You can generate pages dynamically (server side) as long as you don't intend to change them after. Think about the process in the same way you would build landing pages for SEO purposes. Twitterbot is in many ways really similar to Googlebot and similar indexers.
See https://dev.twitter.com/blog/twitter-cards-tips-tricks for a few examples of sites doing this.

Where to place master page in asp .net web project?

I have this folder structure in my project.
Web,WebControls along with other folders. My .aspx pages are in Web folder and master page is in WebControls folder. This has caused some problems.
In anchor tags with href='#' and onclick='SomeJavascriptFunction', href value becomes /WebControls/# and this results in 404.
In anchor tags href='SomeJavascriptFunction', when user clicks on the link, it looks for /Web/SomeJavascriptFunction and this is obviously not found there.
All I want to know is what is the best practice to place Master page in the project? If I can go ahead with the present structure, how can I overcome above problems?
Thank you very much in advance.
As you're happy with the solution posted in the comments, I'm reproducing it here:
Where to leave the MasterPages:
There's no specific rules regarding that, but just leave them in the same folder you have the pages. It will make your life easier.
How to get rid of the problems posted:
I wouldn't set the href attribute with the javascript function as it is. I would rather do that: href="javascript:void()" onclick="myJavascriptFunction()".
Why my Javascript function is returning undefined:
The document ready is used to attach event handlers, not to define functions. These should stay off this block.

Add query string to script tag link in asp.net to break Cache

Hope someone can help me on this. I manage an ASP.Net website and usually update script files and css files very often. I add current time appended into a single string as a query string parameter (eg: profileImage.jpg?123021) which makes the browser to look for the file without getting it from cache.
How can I do the same thing to all script tags and css links from the server side so that it loads the latest version of the file.
Any help appreciated.
Amila
If your asp.net website uses Master Pages, it should be easy to make these changes in the Master Page file. Look a file with the extension .master. If you are not sure how to make the specific edit, post the <head> area from the master page markup in a new question.
MSDN Master Pages: http://msdn.microsoft.com/en-us/library/wtxbf3hh(v=vs.100).aspx
If your site does not use master pages, then you'd need to create some server-side logic that affects the <head> section of the page. Below are some links to related QA's about injecting script and styles from the server side.
ASP.NET: How to (programmatically) attach a <script> tag, containing a link to .js, to <head>?
How to Add script codes before the </body> tag ASP.NET

Loading external content with jquery or iframe?

Hiho,
There's an existing website that i need to include into another site which goes like this:
a.mysite.com
and i need to fetch content from this site in my
www.mysite.com
website...
As i need to access the content of the iframe the Same origin policy produces a problem here.
What i did was to configure mod_proxy on Apache to proxy pass all requests from
www.mysite.com/a
to
a.mysite.com
This will work fine...but my problem is that im not sure what the best way would be to include those pages.
1. Idea
As the content of the iframe is a full featured site with a top navigation...left navigation etc....i would need to change the page template to only show the content box to be able to integrate that page in the iframe.
2. Idea
I could just load the DIV where the content lies through JQuery.load() and integrate it into my site.
What is the best way to accomplish such a task? How bad is both ideas from the SEO point of view?
Unless it involves significant rework, the best solution is to combine the two into a single HTML page on the server side (using server-side includes).
Advantages:
No problems with SEO as it's delivered as a single page. Content in iFrames and content loaded via AJAX (with an associated link in the HTML) are traversed, but only the link, not the content itself is associated with the main page. See: http://www.straightupsearch.com/search-marketing/best-practices/seo_iframes_a_g/
Faster page load - either of your suggestions will cause the main page to be loaded first before the other content is loaded.
No reliance on Javascript - your second method will fail completely if javascript is not supported / turned on.
Include all JS and CSS only once - your first method will require these to be duplicated in the <head> of each page. This is more of a long term advantage if you wish to achieve full integration of site "a". However, it can be a disadvantage initially, see below.
Disadvantage:
May cause conflicts with scripts and CSS between the two pages. However, this same problem exists with your second method.
If you must choose between either of the two options you proposed, I would not select the second as others have suggested. Significant amounts of static content should never be loaded via Ajax, and in this scenario gives you no additional benefits. At least iFrames guarantee no JS and CSS conflicts.
Use the 2nd approach (jQuery.load) and if you're working with HTML5, for browsers that support the History API you can change the URL to whatever the content is for that div.
Check out https://github.com/blog/760-the-tree-slider for an example of how github did it for their tree slider.
EDIT:
I am not sure how using an iFrame whose src points to your own domain affects search rankings but at best it's a grey area. I would assume that possibly some pagerank would trickle from the parent to the child but I have no clue how it would work for instance if a blogger linked to your page with the iframe that pointed to another page. This would be a pretty good question to ask at the Webmaster Help Forum
Always say no to iframes. jQuery+Ajax all the way.

Categories

Resources