41

How can I trigger a method at page load like tab?
for example:

<div id='wraper'>
<!-- div id menu not load -->
<div id="menu">
  <a href='#'>test</a>
  <a href='#'>test</a>
  <a href='#'>test</a>
</div>

<!-- load this content  -->
<div id="content">
konten
</div>
  
</div>  

Thanks

4
  • What exactly are you trying to do? What's the "method" you're trying to run? I don't know what vue.js is, but JavaScript has the built-in window.onload event to have code that executes when the page loads. (just google "window.onload" for info) Commented Jan 28, 2016 at 15:03
  • I'm trying to make it so when I click on a link in a HTML page, it dynamically loads the requested page into a div with jQuery. How can I do that? Commented Jan 28, 2016 at 15:11
  • Ah, okay! If you're trying to load an entire web page inside another page, that sounds like you'll want to use something called iframes (google them). Basically, you'll have an iframe, and it'll have a name (let's suppose you name it "my_iframe"). Then, in your a tags, you'll add target="my_iframe", and they will automatically open in the iframe. Commented Jan 28, 2016 at 15:12
  • yah like that but give me example for that Commented Jan 28, 2016 at 15:19

2 Answers 2

69

For vue >= 2.0 use mounted and for previous version use ready.

vm=new Vue({
  el:"#app",
  mounted:function(){
        this.method1() //method1 will execute at pageload
  },
  methods:{
        method1:function(){
              /* your logic */
        }
     },
})
Sign up to request clarification or add additional context in comments.

2 Comments

Its better to use created() if you are not going use DOM access, as its run earlier in the process, and if you do remote api calls, they are executed earlier
@Ferrybig true, however - in this instance, it looks like the OP is working with the DOM.
23

This syntax is working fine:

export default {
    mounted() {
      this.myMethod();
    },
    methods: {
      myMethod() {
        return 'hello world!';
      }
    },
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.