0

I am creating a plugin for a template. After publishing the template to the web, there is an attribute inside an inserted script that I need to get its value and use it in my plugin.

Is there a way to do it with JS/jQuery?

Here is the part of the page in which the attribute is located:

Platform.Utils.initWidget('#skyline', function (elem) { 
    new Website.tool.Constantin(elem, {
        event: 'click',
        transitionDuration: 93000
    }); 
});

I need to find a way to search the html and get the value for transitionDuration i.e 93000.

Additional comment:

This code is generated by the template and I have no control on changing how it is formed. I inspected the html, and the template places the code as a JS code ( "the code" ) somewhere in the body. So I assumed that there might be a way that the plugin that I am making could be able to read the html, find the attribute and use it to get the same transition duration that the html defines on its elements.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Bondsmith
  • 1,343
  • 3
  • 13
  • 27
  • 1
    `Here is the part of the html` - posts JavaScript. *What*. – Siguza Aug 03 '15 at 08:23
  • 2
    That isn't html you have showing. That's javascript... – NewToJS Aug 03 '15 at 08:24
  • Add your html code and specify the attribute you need from the html file – siva Aug 03 '15 at 08:27
  • Show the HTML. You can use document.getElementById, document.querySelector and jQuery assuming the html lends itself to be searched – mplungjan Aug 03 '15 at 08:27
  • To the above users: please, read carefully that he said. He is mistaken in HTML but he need to read an attribute in the code that he share. – Marcos Pérez Gude Aug 03 '15 at 08:29
  • @MarcosPérezGude I can see what the OP is asking, I'm just waiting for the html to go with this javascript. Also, will you be wanting to do this for just one element or multiple elements? – NewToJS Aug 03 '15 at 08:37
  • I think that OP doesn't distinct HTML of Javascript. He thinks that his code is an HTML code, and he needs to read transitionDuration property. Otherwise, this is a post that needs to be better explanation or tag it as closed because is poor and bad explained. – Marcos Pérez Gude Aug 03 '15 at 08:42
  • @Marcos Pérez Gude I tried to explain it as a comment below, please read the comment. – Bondsmith Aug 03 '15 at 08:49
  • @MarcosPérezGude So why would the OP tag javascript and jQuery rather than tagging html? See, we're just as confused as you and waiting for the question to be edited. I think it's safe to assume that this question will not get a good answer until the OP displays the html. – NewToJS Aug 03 '15 at 08:49
  • @NewToJS I added a new comment to the post, please read the comment and let me know if you need more explanation. – Bondsmith Aug 03 '15 at 08:54
  • So you need to inspect the page for a SCRIPT inserted somewhere? – mplungjan Aug 03 '15 at 08:55
  • @mplungjan Yes, and use its value in my script. – Bondsmith Aug 03 '15 at 08:56

2 Answers2

1

After re-reading and seeing your comments, I assume your template inserts a script somewhere and you want to get at the transitionDuration with the plugin.

As in

    var scripts = document.getElementsByTagName("script"), duration=0;
    for (var i=0;i<scripts.length;i++) {
      var text = scripts[i].textContent;
      if (text.indexOf("transitionDuration") !=-1) { 
        duration = parseInt(text.split("transitionDuration:")[1],10);
        break;
      }
    }
    alert(duration);
    <script>
    Platform.Utils.initWidget('#skyline', function (elem) { 
      new Website.tool.Constantin(elem, {
        event: 'click',
        transitionDuration: 93000
      }); 
    });
    </script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks, that is exactly what I meant. I have used your code, but it returns "NaN", do you have any idea why? – Bondsmith Aug 03 '15 at 09:11
0

You can get CSS values in javascript and jQuery but in this case, is better if you store the value in a variable.

var transDuration = 93000;
Platform.Utils.initWidget('#skyline', function (elem) { 
   new Website.tool.Constantin(elem, {
      event: 'click',
      transitionDuration: transDuration
   }); 
});

alert(transDuration);

You can access to the transDuration variable when you need it.

If you need to read a CSS value, take a look at this:

Get a CSS value with JavaScript

Good luck

Community
  • 1
  • 1
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • Thanks for the advice. However, unfortunately this code is generated by the template and I have no control on changing how it is formed. I inspected the html and the template places the code as a JS code () somewhere in the body. So I assumed that there might be a way that the plugin that i am making could be able to read the html, find the attribute and use it to get the same transition duration that the html defines on its elements. – Bondsmith Aug 03 '15 at 08:46