{"id":84,"date":"2009-09-04T01:18:00","date_gmt":"2009-09-04T01:18:00","guid":{"rendered":"http:\/\/localhost\/tfcom_wp\/2009\/09\/04\/jquery-ajax-file-include\/"},"modified":"2009-09-04T01:18:00","modified_gmt":"2009-09-04T01:18:00","slug":"jquery-ajax-file-include","status":"publish","type":"post","link":"https:\/\/www.tech-freaks.com\/java\/code-snippets\/jquery-ajax-file-include.html","title":{"rendered":"A Jquery way to include file"},"content":{"rendered":"<p style=\"text-align: left;\">Although all server side components provide the option of including file like &lt;jsp:include&gt; etc, having a way to include files using clients side script can come handy if the pages are made of plain old HTML.<\/p>\n<p style=\"text-align: left;\">We provide a code for explaining how Jquery can be used to include files.<\/p>\n<p style=\"text-align: left;\">Main file:<\/p>\n<pre class=\"language-javascript\"><code>&lt;html&gt;\r\n &lt;head&gt;\r\n  &lt;script type=\"text\/javascript\" src=\"static\/jquery-1.3.1.js\"&gt;&lt;\/script&gt;\r\n  &lt;script type=\"text\/javascript\"&gt;\r\n   \/\/Gets called after the page is completely loaded\r\n   $(document).ready( function() {\r\n    \/\/loads include page using AJAX passing parameters and executes the function after completion\r\n    $(\"#include\").load(\"incJquery.jsp\", {param1:25, param2:30}, function() {\r\n     alert(\"Loading include file completed!\");\r\n    });\r\n   });\r\n  &lt;\/script&gt;\r\n &lt;\/head&gt;\r\n &lt;body&gt;\r\n  &lt;div id=\"include\"&gt;Loading....&lt;\/div&gt;\r\n  Welcome to Jquery &lt;br\/&gt;\r\n  You see me in the main file!\r\n &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<p style=\"text-align: left;\">Include file:<\/p>\n<pre class=\"language-markup\"><code>&lt;%@ taglib uri=\"http:\/\/java.sun.com\/jstl\/core\" prefix=\"c\" %&gt;\r\n&lt;h2&gt;Am from include file, thanks to Jquery!&lt;\/h2&gt;\r\nValue of Parameter1 is &lt;c:out value=\"${param.param1}\"\/&gt;\r\n&lt;br\/&gt;Value of Parameter1 is &lt;c:out value=\"${param.param2}\"\/&gt;&lt;p\/&gt;<\/code><\/pre>\n<p style=\"text-align: left;\">The jquery load function basically is an AJAX call which loads the content and inserts it into the div with id=include.<\/p>\n<p style=\"text-align: left;\">The load used uses the three parameter function. The first parameter being the URL, second is data (which is basically a key\/value pair object) and lastly a function. The data (key\/value) gets passed to the URL as parameters. The include file can access the parameters like any GET parameters received.<\/p>\n<p style=\"text-align: left;\">Add both the files in a Java application server context. The files should be placed in same directory. Access the main file from browsers using a localhost url for going through the application server.<\/p>\n<p style=\"text-align: left;\">See the contents loaded from include file and also the parameter contents received in the include file.<\/p>\n<blockquote>\n<p style=\"text-align: left;\"><em>Do not try to load pages from a different domain using the above script. It will not work and will provide a cross domain access error.<\/em><\/p>\n<\/blockquote>\n<p style=\"text-align: left;\">\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although all server side components provide the option of including file like &lt;jsp:include&gt; etc, having a way to include files [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-code-snippets"],"_links":{"self":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/comments?post=84"}],"version-history":[{"count":0,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}