0

I am working with dynamics and I am trying to pull in a variable from the form and use it to create a dynamic URL Here is my code:

<html>
  <head>
    <meta charset="utf-8">
    <script src="new_/jquery.js"></script>

    <script>
      $( document ).ready(function() {
        var $productid = attr("src",parent.window.Xrm.Page.getAttribute('new_productid').getValue());
      });
    </script>
  </head>
  <body style="word-wrap: break-word;">
    <div style="align: center;">
      <script type="text/javascript">
        document.write('<a href="http://www.mywebsite.com/products/'+productid+'">Link to some site</a>');
      </script>
    </div>
  </body>
</html>

Can anyone see what I am doing wrong?

4
  • 1
    Most likely it is $productid vs productid. Remove the $. Also, in the future, you should explain what is happening vs what you want/expect to happen. All you're saying now is that something's wrong. It's usually hard to find what's wrong when you don't know what's happening. Commented Jul 9, 2014 at 19:51
  • also what is the attr function you are calling in the .ready callback, JS has no native attr global function Commented Jul 9, 2014 at 20:02
  • Sorry guess I know even less than I thought about what I'm doing. I used some example code a friend gave me and wrote this, and now that friend is not available to ask for help. Commented Jul 9, 2014 at 20:18
  • What is happening instead of showing my Link it shows absolutely nothing. Commented Jul 9, 2014 at 20:24

2 Answers 2

1

you have 3 problems:

  1. $productid vs productid
  2. $productid is declared inside the callback function so is not in the same scope as your document.write call
  3. Even if $productid was in global scope your document.write call will more than likely happen before your $(document).ready callback so $productid will still not contain the value you expect.

So do your js work inside the ready callback, and put a placeholder anchor in your html that you can change once it is ready

HTML

<div style="align: center;">
  <a href="#" id="linkToChange">Some text</a>
</div>

JS

$( document ).ready(function() {
    var $productid = attr("src",parent.window.
                                Xrm.Page.
                                getAttribute('new_productid').getValue());
    $("#linkToChange").attr("href","http://www.mywebsite.com/products/'+$productid);
});

Also not sure what the function attr you are calling is, as you do not define it anywhere so you are more than likely getting an error for this as well

Sign up to request clarification or add additional context in comments.

Comments

0

Your script is running before the variable is even declared, you declare the var in the jQuery ready() function, that is occuring AFTER that script block is executed. Move your js script to a function and have it called after the var is declared in the ready() function.

1 Comment

On top of that, productid wasn't ever defined anywhere and even if it was in place of $productid, $productid wouldn't be in the scope of the other script even if it ran after the ready()

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.