How to load div on var hash example #index2? - javascript

Its my button:
<div id="page-wrapper"></div>
$("#index2").click(function(){
$(document).ready(function() {
$('#page-wrapper').fadeOut('fast', function(){
$('#page-wrapper').load("index2.php", function(){
$('#page-wrapper').fadeIn('fast');
});
});
});
});
Its successfully work and no problem but this does not work if the page refreshes
And that's when you refresh the page on the www.domain.com/index.php#index2 page
Now how can i load again index2.php to #page-wrapper on refresh?

It would be better approach to use hashchange event to handle # anchor changes. Try like this;
$(window).on('hashchange', function() {
var hash = window.location.hash;
var loadUrl = hash.substr(1) + ".php";
$('#page-wrapper').fadeOut('fast', function(){
$('#page-wrapper').load(loadUrl, function(){
$('#page-wrapper').fadeIn('fast');
});
});
});
To fire hashchange event at first page load you can try;
$( document ).ready(function() {
if (window.location.hash) {
$(window).trigger('hashchange')
}
});

Related

How to change the page content without reloading

I want to know how can I change the content of a div (for example: <div id="MyDiv"></div>) when I click any link for an HTML file with PHP code?
Now, I tried to do this:
$(function(){
$("a").on("click", function () {
var href = $(this).attr('href');
$("#MyDiv").load(href);
});
});
But it replaces the content of the whole page.
Yeah, I need to prevent the default action, this will do what I want:
$(function(){
$("a").on("click", function () {
event.preventDefault();
var href = $(this).attr('href');
$("#MyDiv").load(href);
});
});

Detect click in iframe

I having trouble detecting clicks in an iframe(iframe id is '#ptifrmtgtframe' and tag id is '#CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH'). I have tried:
$('#ptifrmtgtframe').click( function() {
$('#CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH').click( function() {
console.log("clicked");
});
});
I have also tried
var htmlDocument = document.querySelector('#ptifrmtgtframe').contentDocument;
$(htmlDocument).contents().find('#CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH').on('click', function() {
console.log("clicked");});
Iframes are a bit different in that you have to load them and get their contents before you can do anything with it:
$('#ptifrmtgtframe').on('load', function() {
var iframe = $(this).contents();
iframe.find('#CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH').click( function() {
console.log("clicked");
});
});

preventDefault not working after fadeout then load then fadein new href content into div

I've searched and serached and nothing really seems to answer what I'm looking for.
I'm pulling in html pages into a div. I finally got it to fadeout, load new href content, then fade in the new content. However, I can't get it to preventDefault on the link.
Here's my code. Any help is greatly appreciated!
$(document).ready(function() {
var url = $(this).attr("href");
$('#container').css('display', 'none');
$('#container').fadeIn(1000);
jQuery('a').click(function(e){
e.preventDefault();
$('a').removeClass('current');
$(this).addClass('current');
$("#container").fadeOut('1000',function(){
$('#container').load(url);
}).fadeIn('1000');
});
})
You need to call this fadeIn inside the load callback.
$(document).ready(function() {
var loading = false;
$('#container').css('display', 'none');
$('#container').fadeIn(1000);
$('a').click(function(e){
if(loading) return false;
e.preventDefault();
loading = true;
var url = $(this).attr("href"),
cont = $("#container"); //cache selector
$('a').removeClass('current');
$(this).addClass('current');
cont.fadeOut(1000, function(){
cont.load(url, function() {
cont.fadeIn(1000, function() {
loading = false;
});
});
});
});
});

setting variables in javascript

I need to create on snippet of javascript that, based on an A tags id, the window will navigate to the proper html file. I've got something together that when I look at it, it should work but for some reason it doesnt. Here's what I got.
<script>
$(document).bind('pageinit', function() {
$('a').each(function (index){
var elementId = $(this).attr("id");
elementId= elementId + '.html';
$(function(){
$(elementId).click(function (event) {
event.preventDefault();
window.location.assign(elementId);
});
});
});
});
</script>
This part is so that I can load an external html in an ios web app with out exiting the web app window
$(function(){ $(elementId).click(function (event) {
event.preventDefault();
window.location.assign(elementId);
Did I write the variable wrong some how? Any help would be appreciated
I'll take a wild guess:
$(function(){
$('a').on('click', function(e) {
e.preventDefault();
window.location.assign(this.id + '.html');
});
});
This is a simplified version of what you have there...
<script>
$(function() {
$("a").on("click", function(e) {
e.preventDefault();
window.location.href = this.id + ".html";
});
});
</script>

jQuery not working in IE, works in other browsers

Currently coding a mates portfolio and not to my surprise the code isn't loading in IE!
I'm coding it using standard AJAX, here's the relevant jQuery:
//ajax shtuff
$(window).load(function() {
// Ajax Cache!
$.ajaxSetup ({
cache: false
});
var $loadW = '<div id="whiteLoader" />';
var $loadurl = $('.current').attr('href');
// Initial Page Load
$('#con').prepend($loadW);
$('#main').fadeOut('slow', function() {
$(this).load($loadurl + ' .page', function() {
$(this).parent().find('#whiteLoader').fadeOut('slow', function() {
$(this).parent().find('#main').fadeIn('slow').css({background: 'red'});
$(this).remove();
});
});
});
$('nav ul li a').each(function() {
$(this).click(function(e) {
var $loadW = '<div id="whiteLoader" />';
var $loadurl = $(this).attr('href');
// Prevent default hotlink
e.preventDefault();
// Add the current state
$('*').removeClass('current');
$(this).addClass('current');
// Load the Page
$('#main').fadeOut('slow', function() {
$('#con').prepend($loadW);
$('#main').load($loadurl + ' #main', function() {
$('#whiteLoader').fadeOut('slow', function() {
$('#main').fadeIn('slow');
$(this).remove();
});
});
});
});
});
});
Literally have no idea why this doesnt work lol, here's a link to the live page (I've put the background as red just to show you the area.)
Also the reason the initial page is using the 'this' method is because I was testing it both ways.
http://212.7.200.35/~tfbox/zee/
have you tried
$(document).ready(function() {
// Stuff to do as soon as the DOM is ready;
});
instead of window.load?
Often IE has trouble styling / selecting any of the new HTML5 elements such as section and nav. Try using something like this or simply using a div

Categories

Resources