pagination by using jquery - javascript

I would like to make pagination in jQuery, but it is not working. JavaScript is working, but jQuery code is not working. I think that the variable num is the problem.
What is the problem in this code?
jQuery
<script type="text/javascript">
jQuery(document).ready(
function fn_page(num) {
document.form1.target = "_self";
document.form1.action = "";
document.form1.page.value = num;
//document.form1.submit();
/* jQuery("form1").attr('target', '_self').submit(); */
}
);
</script>
JavaScript
<li>{$p_num}</li>

You don't need to wrap this function in (document).ready.
Change it to this (assuming the id of your form is id="form1":
<script type="text/javascript">
function fn_page(num) {
document.forms['form1'].target = "_self";
document.forms['form1'].action = "";
document.forms['form1'].page.value = num;
document.forms['form1'].submit();
}
</script>
And really, since you're using jQuery, you should just stay true to your abstraction:
<script type="text/javascript">
function fn_page(num) {
jQuery("#form1").attr("target", "_self");
jQuery("#form1").attr("action", "");
jQuery("#form1 input[name=page]").val(num);
jQuery("#form1").submit();
}
</script>

Related

How do I add this code to what I already have in oder for this code to work

I would like to implement the second code into the first one in order for it to work as intended. I am unsure how to do this
Current code:
<script type="text/javascript">
!function(){
var campaign_link = ""; // REPLACE WITH YOUR LINK
var t;
try{
for(t=0;10>t;++t)history.pushState({},"","#");
onpopstate=function(t){t.state&&location.replace(campaign_link + window.location.search.substring(1))}}
catch(o){}
}();
</script>
the code I would like to add to existing code;
window.addEventListener("popstate", function(e) {
window.location.href = location.href;
});
You can just add this function to your current script like so. However you may want to store the loction.href in a var as opposed to window.location
<script type="text/javascript">
function(){
var campaign_link = ""; // REPLACE WITH YOUR LINK
var t;
try{
for(t=0;10>t;++t)history.pushState({},"","#");
onpopstate=function(t){t.state&&location.replace(campaign_link + window.location.search.substring(1))}}
catch(o){}
}();
window.addEventListener("popstate", function(e) {
window.location.href = location.href;
});
</script>

Chrome Extension - How to pass variables from JS to popup.html?

I have some variables in the following JS:
document.addEventListener('DOMContentLoaded', function (){
document.getElementById('btn4').addEventListener('click', getbg);
});
getbg = function()
{
chrome.runtime.getBackgroundPage(
function (bg) {
var allcompanynames = bg.companynames;
alert(allcompanynames)})
}
As you can see, the variable is "allcompanynames".
However, how do I pass them and show it on the popup.html page?
I have tried
<script type="text/javascript" src="companynames.js"></script>
<p id="allcompanynames"></p>
no luck. What's wrong?
document.addEventListener('DOMContentLoaded', function (){
document.getElementById('btn4').addEventListener('click', getbg);
});
getbg = function()
{
chrome.runtime.getBackgroundPage(
function (bg) {
var allcompanynames = bg.companynames;
alert(allcompanynames)})
document.getElementById("allcompanynames").innerHTML(allcompanynames)
}
I'm guessing you should add that last line after displaying the pop up to add the content into the page.
Write your code in this way
var background = chrome.extension.getBackgroundPage();
var allcompanynames = background.companynames;
alert(allcompanynames)

Combining two script in jquery/javascript

I have two script in javascript/jquery. How i will combine in one. On body load this function run
<script type="text/javascript">
window.onload=function() {
var tId = setInterval(function() {
document.getElementById("link1").disabled=!navigator.onLine;
},250);
</script>
<script>
var status = navigator.onLine;
if (status) {
$("#link1").removeClass('diconnected').addClass('connected');
$("#link1").data('disabled',true);
} else {
$("#link1").removeClass('connected').addClass('diconnected');
}
}
</script>
$(document).ready(function(){
var status = navigator.onLine;
if (status) {
$("#link1").removeClass('diconnected').addClass('connected');
$("#link1").data('disabled',true);
} else {
$("#link1").removeClass('connected').addClass('diconnected');
}window.onload=function() {
var tId = setInterval(function() {
document.getElementById("link1").disabled=!navigator.onLine;
},250);
}});
try this
Your first script is missing a closing bracket }
If you add in a } you can remove the closing and opening script tags.
Additionally, you may want the second bit of code within the window.onload function, which would mean you could put the closing bracket } after the 2nd scripts code

javascript will not work onload

The javascript on the page needs to work onpage load. So I tried adding the document ready function into the code. It doesn't seem to work.
http://janeucreative.com/daddychallenge/bag.html
<script>$(document).ready(function() {
function addItem(item) {
var itemInCart = item.cloneNode(true);
itemInCart.onclick = function() { removeItem(this); };
var cart = document.getElementById("cart");
cart.appendChild(itemInCart);
}
function removeItem(item) {
var itemInItems = item.cloneNode(true);
itemInItems.onclick = function() { addItem(this); };
var cart = document.getElementById("cart");
cart.removeChild(item);
}
init();
});</script>
Any advice would be much appreciated! I'm very new to javascript and just trying to learn it a step at a time.
First, you'll want to have your page using a more modern version of jquery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.js" ></script>
Then, AFTER linking the jquery, which you have done correctly, you can rewrite your code like this:
<script>
function init(toAdd, toRemove){
addItem(toAdd);
removeItem(toRemove);
}
function addItem(item) {
var itemInCart = item.cloneNode(true);
itemInCart.on("click",function(){removeItem(this);});
$("#cart").appendChild(itemInCart);
}
function removeItem(item) {
var itemInItems = item.cloneNode(true);
itemInItems.on("click", function(){addItem(this);});
$("#cart").removeChild(item);
}
init($("#myAddedItem"), $("myRemovedItem"));
</script>
This way, you'll have your functions addItem and removeItem available elsewhere on the page. You currently seem to have them set up to run only once, from the page initialization.
I'm not sure what you're doing with cloneNode.

applying a var to an if statement jquery

Need to apply a var to a statement if its conditions are met, this syntax isn't throwing errors but its not working.
<script type="text/javascript">
$(document).ready(function(){
var action_is_post = false;
//stuff here
$(this).ready(function () {
if ($("#stepDesc0").is(".current")) {
action_is_post = true;
}
});
//stuff here
</script>
should I use something other than .ready? Do I even need the $(this).ready(function ()... part? I need it to apply the var when #stepDesc0 has the class current.
$(document).ready(function(){
var action_is_post = false;
$("form").submit(function () {
action_is_post = true;
});
if ($("#stepDesc0").is(".current")) {
var action_is_post = true;
}
window.onbeforeunload = confirmExit;
function confirmExit()
{
if (!action_is_post)
return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the form.';
}
});
<script type="text/javascript">
$(document).ready(function(){
var action_is_post=$("#stepDesc0").is(".current");
});
</script>
If you want the variable to be accessible outside the $(document).ready(function(){..., then you'll need to declare it outside the statement like this:
<script type="text/javascript">
var action_is_post;
$(document).ready(function(){
action_is_post=$("#stepDesc0").is(".current");
});
</script>
HTML (in order to test it):
Show value
$(function() {
var actionIsPost = $('#stepDesc0').is('.current');
alert( actionIsPost );
});
If you are adding the current class to #stepDesc0 on an event then put the .is check in the event handler.

Categories

Resources