Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:17:02+00:00 2026-06-18T07:17:02+00:00

I have this function that (on page load) changes my margin using the css

  • 0

I have this function that (on page load) changes my margin using the “css” jQuery method…

function page_change() {
  var h = window.location.hash;

  switch (h) {
    case 'home':
      $('.page-slide-box').css({marginLeft: 0});
      break;
    case 'history':
      $('.page-slide-box').css({marginLeft: '-820px'});
      break;
    // more cases here.....
  }
}

…but after the page is loaded, I’d like to animate the change instead. I was thinking I could alter the existing function using replace() (rather than writing another redundant function), like so:

window.onhashchange = function() {
  var get = page_change.toString();
  var change = get.replace(/css/g, 'animate');
  page_change();
}

This successfully changes all instances of “css” to “animate” in my page_change() function. How do I get this function to change dynamically once I’ve replaced the strings? Or is this just a bad idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:17:03+00:00Added an answer on June 18, 2026 at 7:17 am

    In your example, I’d say this is a terrible idea. Why not simply define 1 function that can do both, and use it accordingly:

    var page_change = function(e)
    {
        var method = e instanceof Event ? 'animate' : 'css';
        switch (location.hash)
        {
            case 'home':
                $('.page-slide-box')[method]({marginLeft: 0});
            break;
            //and so on...
        }
    };
    

    call this function directly, and it’ll set the css, use it like so:

    window.onhaschange = page_change;
    

    and it’ll animate instead of use the css method. Easy

    If you want to test this easily, you could try this:

    var page_change = function(e)
    {
        var method = e instanceof Event ? 'animate' : 'css';
        console.log(method);
    };
    document.body.onclick = page_change;
    page_change();//logs css
    //click the body and... 
    //animate will be logged
    

    That’s, basically, how this works.
    The added benefit of defining a function like this (anonymous function, assigned to a variable or referenced by a var) is that you can easily assign a new function to that same variable:

    var myFunc = function(){ console.log('foo');};
    myFunc();//logs foo
    myFunc = function(){console.log('bar')};
    myFunc();//logs bar
    

    This might work for you, too… of course. You can even store the old function:

    var myFunc = function(){ console.log('foo');};
    myFunc();//logs foo
    var oldMyFunc = myFunc;
    myFunc = function(){console.log('bar')};
    myFunc();//logs bar
    oldMyFunc();//logs foo
    

    Play around with this for a while, to find the approach that fits your needs best (it could well be a combination of the things I talked about in this answer)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function in jQuery that gets data from a page with POST,
I have a javascript function that executes at page load, which is like this:
I have a callback function that changes window.location.href = url; however, this callback is
I have a jquery function that changes an image depending on scroll position. This
I have this jquery function that works, except I need to add something. I
I have this jQuery ajax navigation tabs plugin that I created using some help
I have this function that I would like to condense into some iterator. How
I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have this function that takes the input of a, runs a calculation and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.